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