1

I'm trying to add (well, append really) the letters in the alphabet to an empty char array. However, I appear to run into some sort of pointer issue I don't understand, as my array contains only the last character. I tried moving the letter char outside of the for loop, but the compiler didn't like that. I also looked on here about how to create a list of all alphabetical chars, and one of the better answers was to type them all in 1 at a time. However, my problem means I don't fully understand for loops and pointers in C, and I want to.

#include <stdio.h>

int main(void) {

    char *empty_list[26];

    for (int i = 0; i < 26; i++){
        char letter = i + 65;
        empty_list[i] = &letter;
    }

    printf("%s", *empty_list);

    return 0;
}
2
  • 2
    It's &letter. Change it to just letter, as well as making empty list just char empty_list[26]' . What you're doing with &letter` and an array of pointers is to make each array entry contain the address of the variable letter, and since that is continuously updated it will only contain the last change made to it, which is the last letter. Commented Mar 20, 2019 at 21:46
  • 1
    Also consider using 'A' rather than 65 in the calculation. It makes it more obvious what your intention is. Commented Mar 20, 2019 at 21:48

3 Answers 3

3

The main problem is your declaration:

char *empty_list[26];

defines an array of 26 pointers to characters. In your current code you assign each element in the array the address of the variable letter. Since that is out of scope when you print it is luck that it prints out the last one, it could equally have printed out garbage or crashed if the code between was complex. It could also have printed out additional garbage after the letter with what you already have since there is no way of knowing whether there is a string terminating character (\0) after the letter. In your existing code printf("%s", *empty_list); prints the first pointer from the array as a null terminated string, which if you ignore the loss of scope and assume the memory contents are still around, will be the last value from the loop since all pointers in your array point to the memory that letter was stored at and that memory has the last value from the loop.

If your intention was to create an array with the letters then it should be:

char empty_list[27];

It needs to be 27 as you need to leave space for the string terminating character at the end. One way to fill that in would be to use:

empty_list[26] = '\0';

after the end of your for loop and before you print the contents of the array (do not include the asterisk here - because it is an array the compiler will automatically take the address of the first element):

printf("%s", empty_list);

As brothir mentioned in the comments when you assign the value of the letter to the element in the array it should be without the ampersand:

empty_list[i] = letter;
Sign up to request clarification or add additional context in comments.

Comments

2

There are a few things wrong with your code.

Firstly, the type of empty_list is presently an array of pointers to char, when it really should be an array of char, since your intent is to print it out as if it were the latter in the call to printf after your loop. char empty_list[26]; is the correct declaration.

Secondly, in your loop, you assign &letter when all you need is letter. Heck, you don't even need the intermediate variable letter. Just empty_list[i] = i + 'A'; will suffice.

Lastly, you are passing empty_list to printf to satisfy a format specifier %s, which expects a null-terminated string. What you need to do is add another element to empty_list and set that to zero:

char empty_list[27];
// populated 0..25 with 'A'..'Z' in your code...

empty_list[26] = '\0';

printf("%s\n", empty_list);
// Output: ABC...Z

Comments

0

With the above help (much appreciated), my working code to create an array of letters in C is below:

#include <stdio.h>

int main(void) {

    // create an array with 1 extra space for null terminator 
    char empty_list[27];

    // add null terminator so string knows when it's finished.
    empty_list[26] = '\0';

    for (int i = 0; i < 26; i++){
        // add 65 to get ASCII value for 'A'
        char letter = A + i;
        // insert each char into the array sequentially
        empty_list[i] = letter;
    }

    printf("%s", empty_list);

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.