Note that there are some explanatory texts on larger screens.

plurals
  1. POArray of Structs strange output?
    primarykey
    data
    text
    <p>Can anyone give me some indication as to why array of structs doesn't print out properly ?</p> <p>I think its something to do with the memory I have allocated to the struct I am unsure !!</p> <p>Using mac osx mountain lion xcode 4 gcc</p> <p>Thanks for any help completely stuck!! (Please have patience I am only a student !)</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;limits.h&gt; #include &lt;ctype.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;sys/types.h&gt; 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("--&gt; X: %s\n",x); printf("--&gt; Y: %s\n",y); Node newNode; newNode.one = x; newNode.two = y; nodes[count]= newNode; count++; } //----------------------------------------------------------------------- //----------------------------------------------------------------------- void print(){ int x; for (x = 0; x &lt; 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 &lt;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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload