user3734085
user3734085

Reputation: 71

Array of Pointers C - NULL POSITION

I am trying to add nodes to a tree with this function. I have use a array of character pointers for the input data. To insert I am incrementing the pointer each time.

node addToTree(node root , char *words[])
{
int count=0;
while( *(words+2))
{
    printf("\n VALLED %d",count++);
    root=addNode(root,*words);
    printf("\n the current word is %s",*words);
    words++;
}
return root;
}

Why does words+2 work and not words or words+1 in the loop condition

Here is the words array

char *words[3]={"3","8","1"};

Upvotes: 2

Views: 87

Answers (2)

Ankur Agarwal
Ankur Agarwal

Reputation: 24758

For simplicity I modified your code by commenting the addToTree function call

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

void addToTree(char **);

int main() {

    char *words[3]={"3","8","1"};
    addToTree(words);    
}

void addToTree(char *words[])
{
    int count=0;
    while( *words)
    {
        printf("\n VALLED %d",count++);
        //root=addNode(root,*words);
        printf("\n the current word is %s\n",*words);
        words++; //this is unsafe
    }
    return;
}

When I tried your code as is worked with *words however there is one problem here that I see. When words is at the last element of the array you still increment it (which is fine) and dereference the pointer ( in the loop condition). The dereference operation is not a good because in practice you should access memory locations that you are controlling your program explicitly. I think it is a good idea to detect how many elements the array has and then only run the loop that many times.

As Tom said you just lucked out (same as me) because seems like there was a NULL stored at the end of the array.

$ ./treeaddso 

 VALLED 0
 the current word is 3

 VALLED 1
 the current word is 8

 VALLED 2
 the current word is 1

For more clarity please study the o/p of this program:

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

void addToTree(char **);

int main() {

    char *words[3]={"3","8","1"};
    addToTree(words);    

}

void addToTree(char *words[])
{
    int count=0;
    while( *words)
    {
        printf("count is %d\n",count++);
        printf("the current value of words is %p\n",words);
        printf("the current value stored in words is %p\n",*words);
        printf("the character is %c\n",**words);
        words++;
    }
    return;
}

$ ./treeaddso 
count is 0
the current value of words is 0x7fff2ce87de0
the current value stored in words is 0x4006b0
the character is 3
count is 1
the current value of words is 0x7fff2ce87de8
the current value stored in words is 0x4006b2
the character is 8
count is 2
the current value of words is 0x7fff2ce87df0
the current value stored in words is 0x4006b4
the character is 1

Upvotes: 0

Tom Zych
Tom Zych

Reputation: 13576

You're iterating through the pointers in words and checking for a zero pointer, but you didn't add a sentinel at the end of the array. Try this:

char *words[] = {"3", "8", "1", NULL};
...
while (*words)

Presumably, words + 2 worked because there happened to be something in memory at that location that equated to a NULL pointer.

Upvotes: 3

Related Questions