Question

Hey guys i have two problems in my c code. I have created a structure named rotor:

typedef struct rotor {
    char characters[26];
    int rotationPos;
} rotor;

I want to create an instance of that struct like this:

        rotor r;
[error] r.characters[26]= {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
        'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
        'Z'};
    r.rotationPos = 5;

and it gives me the following error: "-Syntax erro; -Expected expression before '{'"

i also have this function

void rotate(rotor r) {
    char aux[26];
    int i;
    for (i = 0; i < 26; i++) {
        if (i == 0) {
[error]         aux[26] = r->characters[i];
        } else
[error]         aux[i] = r->characters[i + 1];
    }
[error] r->characters=aux;
}

that gives: "invalid type argument of '->' (have 'rotor')" Can you guys tell me what am i doing wrong please? Thanks!

Was it helpful?

Solution 2

1) You try to use an initialization list to a char, you meant to write

 r.characters= {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
    'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
    'Z'};

but this is not valid C.

edit: as pointed by WhozCraig, prefer memcpy (I forgot the null terminator with strcpy)

char chars[26] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
                   'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
rotor r;
memcpy(r.characters, chars, sizeof(chars));

or

rotor r = {{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
        'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
        'Z'}, 5};

2) Use are using the -> operator on a rotor object, which is not a pointer. Replace it with "."

void rotate(rotor r) {
    char aux[26];
    int i;
    for (i = 0; i < 26; i++) {
        if (i == 0) {
            aux[26] = r.characters[i];
        }
        else
        {
            aux[i] = r.characters[i + 1];
        }
    }
    memcpy(r.characters, aux, sizeof(aux));
}

Your rotate method is suspicious. What is the intended usage ? Edit: OK

OTHER TIPS

You can't initialize your array like that once you've already created a rotor object, because you can't assign to arrays after you define them. You have to do something like this:

rotor r = {{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
        'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
        'Z'}, 5};

For the same reason, this:

r->characters=aux

at the end of your function is never going to work, because you can't assign one array to another. You'll have to copy all the members, either manually, or using something like memcpy().

For the second question, you declare r as rotor, not as a pointer to rotor, so you should use the . operator, not the -> operator, which is for pointers to structs. Alternatively, change your function to accept a pointer, and pass the address of your struct to it (which is usually better than passing copies or large structs around).

Since rotor is a struct, the character array is created along with it. It's not a pointer to a character array, so you can't point it to something else. You can use the syntax Paul mentioned, or you can overwrite the current array.

    char chars[26] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
                       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
    rotor r;
    memcpy(r.characters, chars, sizeof(chars));

Also, when you call the rotate method, rotor will be copied on to the stack. You'll perform the actual rotation, but the struct you performed it on will be lost when you return. You should pass a pointer instead.

void rotate(rotor *r);

Call it using the & operator.

rotate(&r);

In that method you'll also run into the issue of not being able to replace the array. I'll leave that as an exercise to you, but please comment back if you need any further help. (Since rotor is a pointer inside this method, you'll want to use the -> accessor syntax.)

Welcome to Stack Overflow!

As you already set a size in your structure, you can't do an assignation as you do here.

Plus, you should add a '\0' at the end of your char array if you want to read it properly and avoid reading other char of your memory

You can either declare a array of char with its size:

  char array[4] = {'1', '2', '3', '\0'};

Of if you already declared it:

   char array[4];
   strcpy(array, "123\0");

You can read more about memory allocation and the C string functions.

This post should also help you: Assigning strings to arrays of characters

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top