You need to reserve 27 bytes to store 26 characters plus the required zero terminator. Better to do this, and the compiler will automatically allocate enough space.
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
You can use memcpy() to transfer bytes or characters from one memory location to another.
Is there any difference between these two initializations?
Two differently named character array variables are created.
ToddL1962:
If letters is declared as const then probably no need to.
I think jimLee was referring to the return value of strstr(). If you run that code a second time, for example, pmn will be null because alphabet will not contain the string "mn" anymore.
christop:
I think jimLee was referring to the return value of strstr(). If you run that code a second time, for example, pmn will be null because alphabet will not contain the string "mn" anymore.
jremington:
You need to reserve 27 bytes to store 26 characters plus the required zero terminator. Better to do this, and the compiler will automatically allocate enough space.
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
I thought that char arrays position started from 0, hence from 0-25 i would have the alphabet letters and 26th would have the zero terminator, isnt that correct then?
christop:
I think jimLee was referring to the return value of strstr(). If you run that code a second time, for example, pmn will be null because alphabet will not contain the string "mn" anymore.
Good point, I was not aware of it (new to the language). So I need to make sure the pointer is pointing to a valid content in the alphabet. I'll increase a bit the complexity of the example so I can have more ideas and understand the language better.. The below code is supposed to build a list of alphabets, the start point is the standard alphabet, as the loop goes on we modify it and save within the list. I used the previous idea as @ToddL1962 suggested, but instead of checking if the return of strstr is null I am making sure that the pointer is pointing to not null by an auxiliary char in a recursive way.
Why can't I acess the list content in an specific index outside the loop?
alphabetlist is an array of pointers but you haven't initialized it to any pointers or explicitly told the compiler how many pointers are in the array. Additionally the line:
alphabetlist[i] = alphabet;
Sets the element i in alphabetlist to a pointer to alphabet. Eventually you will have all the elements in alphabetlist pointing to alphabet, which is kind of useless.
You need to study arrays more. You have some fundamental misconceptions about how arrays are accessed and manipulated.