结构数组奇怪的输出?

时间:2013-04-11 19:21:58

标签: c arrays struct

有人能给我一些指示,说明为什么阵列结构不能正确打印出来吗?

我认为它与我分配给结构的内存有关,我不确定!!

使用mac osx mountain lion xcode 4 gcc

感谢任何帮助完全卡住!! (请耐心,我只是一名学生!)

#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>



typedef struct{
char* one;  
char* two;
 } Node;

 Node *nodes;
 int count = 0;


 //-----------------------------------------------------------------------
 void add(char *one,char*two){

 char x[40];
 char y[40];

 printf("reached..\n");

 strcpy(x,one);
 strcpy(y,two);


 printf("--> X: %s\n",x); 
 printf("--> Y: %s\n",y); 

 Node newNode;
 newNode.one = x;
 newNode.two = y;
 nodes[count]= newNode;

 count++;
 }
 //-----------------------------------------------------------------------

 //-----------------------------------------------------------------------
void print(){

 int x;
 for (x = 0; x < 10; x++)
 {
   printf("%d : (%s, %s) \n",x,nodes[x].one, nodes[x].two);
 }

 }

 //-----------------------------------------------------------------------

 //-----------------------------------------------------------------------
 void check(char **arg)
 {

if(strcmp(*arg, "Add") == 0)
{

add(arg[1],arg[2]);

}else if(strcmp(*arg,"print") == 0)
{

 print();
}else{
    printf("Error syntax Enter either: \n Add [item1][item2]\n OR \n print\n");
}

 }
 //-----------------------------------------------------------------------
 void readandParseInput(char *line,char **arg)
 {  

  if (fgets (line, 512, stdin)!= NULL) {  


  char * pch;
  pch = strtok (line," \n\t");
  int count = 0;
  arg[0] = pch;

  while (pch != NULL)
   {
   count++;
   pch = strtok (NULL, " \n\t"); 
   arg[count] =pch;
   }


}else{
printf("\n");
exit(0);
  }

 }

 //-----------------------------------------------------------------------

 int main() 
{


 int size = 100;
 nodes = calloc(size, sizeof(Node));

 int i;
 for(i = 0;i <100; i++){

 printf("%s , %s \n",nodes[i].one,nodes[i].two );
 // nodes[i].one = ".";
 // nodes[i].two = ".";
 }


 char  line[512];             /* the input line                 */
 char  *arg[50];              /* the command line argument      */

while (1) 
{ 
  readandParseInput(line,arg);
  if(arg[0] != NULL){
    check(arg);
    }

 }

return(0);
}

4 个答案:

答案 0 :(得分:0)

您正在指向以下自动变量:

 char x[40];
 char y[40];

add()返回时,这些超出了范围,留下了悬空指针。

您必须将Node::oneNode::two转换为数组,或者在堆上为它们分配内存。

答案 1 :(得分:0)

add()函数中,您无法通过=运算符将一个结构分配给另一个结构...您必须复制它...

memcpy( &nodes[count], &newNode )

答案 2 :(得分:0)

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    char *fn;
}NAME;

#define NAME_LEN 20

int main()
{
    NAME name;

    name.fn = (char *) calloc(NAME_LEN, sizeof(char));

    strcpy(name.fn, "Namco");

    printf("Name: %s\n", name.fn);

    free(name.fn);

    return 0;
}

你不能只在c

中分配这样的字符串
newNode.one = x;
newNode.two = y;

什么是newNode.one引用???

答案 3 :(得分:0)

在功能添加

 newNode.one = x;
 newNode.two = y;

 newNode.one = strdup(x);
 newNode.two = strdup(y);