Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 指向结构的指针_C_String_Pointers_Struct - Fatal编程技术网

C 指向结构的指针

C 指向结构的指针,c,string,pointers,struct,C,String,Pointers,Struct,我正在尝试做一个C程序,它接收字符串并动态地将它们存储到结构中,在传递字符串部分之后,我将显示其中大部分是编写的。但是我在编写指向结构的指针的指针时遇到了麻烦。我试着做一些像我画的图像一样的事情 任何地方都没有指针的分配 你需要这样的东西: lista = (struct Word**) malloc(sizeof(struct Word*)); *lista = NULL; 上面的代码将一个指针分配给指向struct的指针。指向结构本身的指针为空 现在,我不确定你想通过 while(*(&a

我正在尝试做一个C程序,它接收字符串并动态地将它们存储到结构中,在传递字符串部分之后,我将显示其中大部分是编写的。但是我在编写指向结构的指针的指针时遇到了麻烦。我试着做一些像我画的图像一样的事情


任何地方都没有指针的分配

你需要这样的东西:

lista = (struct Word**) malloc(sizeof(struct Word*));
*lista = NULL;
上面的代码将一个指针分配给指向struct的指针。指向结构本身的指针为空

现在,我不确定你想通过

while(*(&lista+i*sizeof(lista)) != NULL){
        i++;
    }
如果要查找指针数组的结尾,假定最后一个指针为NULL,则以下代码可以执行此操作:

while (*(lista + i) != NULL) i++;
此外,代码中还有一些拼写错误。这将编译并运行。但我个人建议使用普通指针数组,也就是说,只需将数组的大小保留在另一个变量中即可

struct Word{
   char* palavra;
   int aparicoes;
} ;
struct Word * createWord(char* str){
   struct Word *newWord = (struct Word *)malloc(sizeof(struct Word));
   newWord->palavra = strdup(str);
   newWord->aparicoes = 1;
   return newWord;
}
int main()
{
  char tempString[1024];
  struct Word** lista;
  int triggrer = 1;
  int i = 0;
  lista = (struct Word**)malloc(sizeof(struct Word*));
  *lista = NULL;
  while (triggrer == 1)
  {
scanf("%s", tempString);

if (strcmp(tempString , "fui") == 0) 
  triggrer = 0;
else 
{

    while(*(lista+i) != NULL){
        i++;
    }

    lista = (struct Word**)realloc(lista, (i+1) * sizeof(struct Word*));
    *(lista+i) = createWord(tempString);
    *(lista+i+1) = NULL;
}
  }
  return 0;
}

谢谢,对不起C和C++ TAGI有问题在哪里?有错误吗?这是什么?问题不在代码中,问题是我没有成功地构建这个系统。所以存在的目的是帮助解决具体问题,而不是为您实现它们。你在哪方面没有成功?这个程序编译时做什么?你想让它做什么呢?那么你用while*lista+i!=空i++;指针没有指向内存的下一个n个位置,这n个位置在内存中的其他位置,这就是为什么使用变量的大小跳转到下一个位置没有意义?是的。lista指向指向指针的第一个指针。lista+1指向指向指针的第二个指针,依此类推*lista指向第一个结构,*lista+1指向第二个结构,依此类推。
struct Word{
   char* palavra;
   int aparicoes;
} ;
struct Word * createWord(char* str){
   struct Word *newWord = (struct Word *)malloc(sizeof(struct Word));
   newWord->palavra = strdup(str);
   newWord->aparicoes = 1;
   return newWord;
}
int main()
{
  char tempString[1024];
  struct Word** lista;
  int triggrer = 1;
  int i = 0;
  lista = (struct Word**)malloc(sizeof(struct Word*));
  *lista = NULL;
  while (triggrer == 1)
  {
scanf("%s", tempString);

if (strcmp(tempString , "fui") == 0) 
  triggrer = 0;
else 
{

    while(*(lista+i) != NULL){
        i++;
    }

    lista = (struct Word**)realloc(lista, (i+1) * sizeof(struct Word*));
    *(lista+i) = createWord(tempString);
    *(lista+i+1) = NULL;
}
  }
  return 0;
}