#include <stdlib.h>
#include <stdio.h>
#define NULL 0
#define amount1 100 //结构体中*arr[]数组长度,用来定义扩充成员个数
typedef struct node
{
struct node *pre; //指向前驱
char *arr[amount1];
struct node *next; //指向后继
}NODE;
NODE *head;
问一下怎么在第一个节点中,从键盘输入N个字符串给*arr[amount1] 空格隔开,回车结束赋值
_(:зゝ∠)_本人也不知道怎么给结构体中的*arr[amount1]申请内存
解决方案:10分
没人回答吗?
你的char *arr[amount1]是个指针数组,每个元素都是指针。
你可以随意申请一个存储空间,例如char* pInput,C用malloc,C++用new,然后用scanf或cin输入你想要的字符串,输入完毕后,把pInput存到arr里面去。例如第一个字符串,就是arr[0] = pInput。
使用完毕后,记得释放pInput的内存。
你的char *arr[amount1]是个指针数组,每个元素都是指针。
你可以随意申请一个存储空间,例如char* pInput,C用malloc,C++用new,然后用scanf或cin输入你想要的字符串,输入完毕后,把pInput存到arr里面去。例如第一个字符串,就是arr[0] = pInput。
使用完毕后,记得释放pInput的内存。
解决方案:50分
供LZ参考,这里先malloc头结点的空间,然后每次能读到一个字符串就malloc一个字符串的空间
#include <stdlib.h>
#include<iostream>
#include <stdio.h>
using namespace std;
#define NULL 0
#define amount1 100 //结构体中*arr[]数组长度,用来定义扩充成员个数
typedef struct node
{
struct node *pre; //指向前驱
char *arr[amount1];
struct node *next; //指向后继
}NODE;
NODE *head;
int main()
{
head = (NODE *)malloc(sizeof(NODE));
int index = 0;
char buffer[64] = {0};
while (scanf("%s",buffer) != EOF)
{
head->arr[index] = (char *)malloc(strlen(buffer) + 1);
strcpy(head->arr[index], buffer);
printf("index = %s\n",head->arr[index]);
index++;
if(getchar() == ""\n"")break;//读到输入的字符串的末尾了,结束循环
}
system("pause");
return 0;
}