2013-04-11 68 views
-1

任何人都可以给我一些指示,为什么数组结构不能正确打印?结构数组奇怪的输出?

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

使用的Mac OSX山狮的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); 
} 
+0

你有什么输出?你可以放置断点并使用观察窗口来查看你的变量在做什么? – infinitloop 2013-04-11 19:25:09

回答

0

你保持指针下面的自动变量:

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

这些走出去的范围时add()回报,让你有悬空指针。

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

0

在你add()功能,可以不通过=运营商分配一个结构到另一个...你将不得不将它复制...

memcpy(&nodes[count], &newNode)

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指这样的字符串???

0

在功能添加

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

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