Home > Blockchain >  Invalid Initializer in C String
Invalid Initializer in C String

Time:04-15

I am testing out a uppercase to lowercase converter in C (I'm relatively new), and I've been having some issues in the main file (which I have attached below). I can add the upperlower.c code, but I don't think it's related to my problem. I am getting some invalid initializer errors, and I would've though this works. I have attached an image of the error message. You can also see in the code that I have commented out some strcpy methods, as they caused some runtime segmentation fault errors. Does anyone know what's going on? Error Message

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "upperlower.h"

#define SAMPLESTRING "AbCdEfGhIjKlMnOpQrStUvWxYz"

int main()
{
    char* lower[] = SAMPLESTRING;
    char* upper[] = SAMPLESTRING;

    // strcpy(*lower, SAMPLESTRING);
    // strcpy(*upper, SAMPLESTRING);

    ToLower(lower, lower);
    ToUpper(upper, upper);

    printf("Lower: %s\nUpper: %s\n", lower, upper);
}

CodePudding user response:

Typing char * lower[N];, you ask your compiler to create a pointer on a character array.

But you want just a characters array:

char lower[] = SAMPLESTRING;
char upper[] = SAMPLESTRING;

CodePudding user response:

From the C Standard (6.7.9 Initialization)

14 An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.

That is you could declare a character array and initialize it with a string literal like

char lower[] = SAMPLESTRING;

However you declared an array of pointers of the type char * instead of a character array. In this case you need to enclose the initializer in braces like

char* lower[] = { SAMPLESTRING };

In the line above there is declared an array with one element of the pointer type char * that points to the first character of the string literal SAMPLESTRING. However using the pointer (the single element of the array) you may not change the string literal. Any attempt to change a string literal results in undefined behavior.

Taking into account this call of printf

printf("Lower: %s\nUpper: %s\n", lower, upper);

where there is used the format string "%s" it seems you are going to deal exactly with character arrays. Otherwise you had ro write

printf("Lower: %s\nUpper: %s\n", lower[0], upper[0]);

So you need to write

char lower[] = SAMPLESTRING;
char upper[] = SAMPLESTRING;

or

char lower[] = { SAMPLESTRING };
char upper[] = { SAMPLESTRING };

instead of

char* lower[] = SAMPLESTRING;
char* upper[] = SAMPLESTRING;

In this case calls of strcpy will look like

strcpy( lower, SAMPLESTRING);
strcpy( upper, SAMPLESTRING);

though the arrays already contain this string literal due to their initializations.

CodePudding user response:

The part you may be confused about is using a global constant as the value for the character array.

Setting the value using a gobal constant is equivalent to setting the value using a local variable, string, or character array.

Any of these methods should be sufficient:

char array[] = globalvariable;
char array[] = localvariable;
char array[] = "this is a string";
char array[] = {'t','h','i','s',' ','i','s',' ','a',' ','s','t','r','i','n','g'}

if you intend on using a pointer, then your array variable used to point towards the array does not need to be an array just a pointer.

Using your code, this would be performed like this:

char * lower;
char * upper;
  • Related