Waifu_Forever
Waifu_Forever

Reputation: 107

Array of Strings some positions are not printing

So, I have an array of strings and the first two positions are not printing, I'm pretty sure it's a size problem, but I don't understand why

#include <stdio.h>

char * switch(int i) {
    char letters[8][20] = {"Caio\0", "Eduarda\0", "Joanderson\0", "Heron\0", "Thiago\0", "Rafaela\0", "Thalisson\0", "Wilton\0"};
    if ((i >=0) && (i<=7)) {
        char*str = letters[i];
        return (str);
    } else {
        return "-";
    }
}


int main()
{
    for(int i = 0; i < 8; i++){
        printf("%d -- %s \n", i, switch(i));
    }

    return 0;
}

Upvotes: 0

Views: 81

Answers (2)

Krishna Kanth Yenumula
Krishna Kanth Yenumula

Reputation: 2567

There are two ways to solve the problem:

  1. Using dynamic memory allocation:

     #include <stdio.h>
     #include <stdlib.h>
    
     char *switch1(int i)
     {
         char **letters = malloc(8 * sizeof(char *));
    
         for (int i = 0; i < 8; i++)
         {
             letters[i] = malloc(20 * sizeof(char));
         }
    
         letters[0] = "Caio";
         letters[1] = "Eduarda";
         letters[2] = "Joanderson";
         letters[3] = "Heron";
         letters[4] = "Thiago";
         letters[5] = "Rafaela";
         letters[6] = "Thalisson";
         letters[7] = "Wilton";
    
         if ((i >= 0) && (i <= 7))
         {
            char *str = letters[i];
            return (str);
         }
         else
         {
            return "-";
         }
      }
    
    int main()
     {
        for (int i = 0; i < 8; i++)
        {
           printf("%d -- %s \n", i, switch1(i));
        }
    
       return 0;
     }
    
  2. Using static memory allocation :

    #include <stdio.h>
    
    char *switch1(int i)
    {
        static char letters[8][20] = {"Caio", "Eduarda", "Joanderson","Heron","Thiago", "Rafaela", "Thalisson", "Wilton"};
        if ((i >= 0) && (i <= 7))
        {
           char *str = letters[i];
           return (str);
        }
        else
        {
           return "-";
        }
    }
    
    int main()
    {
       for (int i = 0; i < 8; i++)
       {
         printf("%d -- %s \n", i, switch1(i));
       }
    
       return 0;
     }
    
  3. I got an error, if I use switch as the name of the function. switch is keyword in C.

  4. Lifetime of letters ends at the end of the switch1 function. We have to use static or dynamic memory allocation to extend its lifetime to entire program.

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 222828

char letters[8][20] = …; defines an array with automatic storage duration. Memory is reserved for it only during the function call. The statement return (str); returns a pointer to an element of this array, and then the memory is no longer reserved for the array. In C’s abstract model of computing, the array ceases to exist. In typical practice, the array is on the stack, but the printf call overwrites it.

You can fix this by defining the array with static storage duration, which reserves memory for it during all of program execution:

static char letters[8][20] = {"Caio\0", "Eduarda\0", "Joanderson\0", "Heron\0", "Thiago\0", "Rafaela\0", "Thalisson\0", "Wilton\0"};

Additionally, it is not necessary to include a null character at the end of a quoted string. String literals automatically include a null character at the end.

Also, string literals define arrays with static storage duration themselves. So, instead of using the strings to initialize an array, you could instead point directly to the strings:

char *letters[8] = {"Caio", "Eduarda", "Joanderson", "Heron", "Thiago", "Rafaela", "Thalisson", "Wilton"};

That array could also be made static. It will work either way (because your function will not return a pointer to an element in the array but will return a pointer, taken from the array, that points to a string with static storage duration), but, if it is made static, it will not have to be initialized every time the function is called.

Upvotes: 1

Related Questions