Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array of structs, inside the while loop I add things to that array, however when I print out the array I get the wrong output? (The last element added is printed out n times, n being the number of things I added)

I have googled this and I think it is because a while loop in Bash creates a subshell, not too sure.

Any help would be much appreciated (please have patience I am only a student!!)

Using Mac OSX mountain lion Xcode 4 gcc

Code:

#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[100];
int count = 0;

void add(char *one,char*two){
    Node newNode = {one,two};
    nodes[count]= newNode;

    printf("one: %s\n",one); 
    printf("two: %s\n",two); 

    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 i;
    for(i = 0;i <100; i++){
        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);
}
share|improve this question
read up on malloc. – Oren Apr 11 at 13:04
It would help you greatly if you properly indent your code. – Shahbaz Apr 11 at 13:04
1  
This is nothing to do with while loops in bash and subshells. Your nodes is an array of type Node, not Node *, so you can't do Node newNode = {one,two}; nodes[count]= newNode;. – Vicky Apr 11 at 13:05
How could I add these items to the struct then to array Vicky ? – Kevin Paton Apr 11 at 18:08

2 Answers

strtok() returns a pointer to different elements within the buffer it was initially passed. This means that all entries in the array will be pointing to different elements of the same buffer, named line. You need to make a copy of the pointer returned by strtok():

in either case, the memory must be free()d when no longer required.

share|improve this answer
What needs to be freed? The buffer line? – Kevin Paton Apr 11 at 17:52

It's because you use the same buffer for all input.

You need to duplicate the strings you put into the structures. Either by using arrays for the strings and strcpy into them, or by using strdup to allocate new memory for the strings and do the copying in one function.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.