简体   繁体   English

C Typedef结构问题

[英]C Typedef Struct Problems

Hey guys i have two problems in my c code. 大家好,我的C代码有两个问题。 I have created a structure named rotor: 我创建了一个名为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? 给出:“'->'(具有'rotor')的类型参数无效”你们能告诉我我在做什么错吗? Thanks! 谢谢!

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. 一旦创建了rotor对象,就无法像这样初始化数组,因为定义数组后无法分配给数组。 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() . 您必须手动或使用诸如memcpy()类的方式复制所有成员。

For the second question, you declare r as rotor , not as a pointer to rotor , so you should use the . 对于第二个问题,您将r声明为rotor ,而不是指向rotor的指针,因此应使用. operator, not the -> operator, which is for pointers to struct s. 运算符,而不是->运算符,它用于指向struct的指针。 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. 由于rotor是一个结构,因此将与字符数组一起创建。 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. 您可以使用Paul提到的语法,也可以覆盖当前数组。

    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. 同样,当您调用rotate方法时, rotor将被复制到堆栈上。 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.) (由于rotor是此方法中的指针,因此您将需要使用->访问器语法。)

Welcome to Stack Overflow! 欢迎使用Stack Overflow!

1) You try to use an initialization list to a char , you meant to write 1)您尝试将初始化列表用于char ,您打算编写

 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. 但这不是有效的C。

edit: as pointed by WhozCraig, prefer memcpy (I forgot the null terminator with strcpy) 编辑:正如WhozCraig所指出的,更喜欢memcpy(我忘记了带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. 2)在转子对象(不是指针)上使用->运算符。 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 编辑:确定

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 另外,如果要正确读取字符数组并避免读取内存的其他字符,则应在字符数组的末尾添加“ \\ 0”

You can either declare a array of char with its size: 您可以声明其大小的char数组:

  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. 您可以阅读有关内存分配和C字符串函数的更多信息。

This post should also help you: Assigning strings to arrays of characters 这篇文章还应该为您提供帮助: 将字符串分配给字符数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM