简单的 C 代码,带有令人烦恼的“赋值类型不兼容” 错误

发布于 2024-07-13 09:30:50 字数 840 浏览 4 评论 0原文

只是一个简单的程序来习惯指针。 该程序应该按照与读取内存的相反顺序将内存的一部分内容放入字符数组中。 IE 查看降序内存地址,我想将其按降序存储在字符数组中。

我的编译器不断告诉我: “赋值中的错误类型不兼容”

realloc函数有关

我做错了什么? 在我看来,“反向”和 realloc 的结果都应该是指向 char 类型的指针?

我的代码:

int main(){
char  first[]="hello mark", strng='h', reverse[]="";
char* ptr=&first[10];
int i=0;
    while(ptr > (&strng-0xf4240)){
        printf("%c", *ptr--);
        reverse = realloc(reverse, (i++ * sizeof(char)));
        reverse[strlen(reverse)-i] = *ptr;
    }
printf("%s", reverse);
return 0;
}

谢谢!

编辑: 抱歉,我把这些错误地发布为下面的评论。

感谢您的帮助,第一条和第二条评论得到了! 我确实有所需的#includes,我只是忘记将它们复制到堆栈溢出中。 你是对的,现在我被困在非空终止的 strlen() 上。 我会自己解决这个问题。 再次感谢!

我说得太早了,编译没问题,但是有逻辑错误。 while 循环将执行一次。 但是,无论 i 的初始值如何,后续循环仍然会失败。 导致失败的行是调用realloc的行

Just a simple program to get used to pointers. The program is supposed to put the contents of a portion of my memory into a character array in reverse order of how the memory is read. I.E. looking at descending memory address, and I want to store it in descending order in a character array.

My compiler keeps telling me:
"error incompatible types in assignment"

On the line with the realloc function

What am I doing wrong? I seems to me that both "reverse", and the result of realloc should be pointers to type char?

My code:

int main(){
char  first[]="hello mark", strng='h', reverse[]="";
char* ptr=&first[10];
int i=0;
    while(ptr > (&strng-0xf4240)){
        printf("%c", *ptr--);
        reverse = realloc(reverse, (i++ * sizeof(char)));
        reverse[strlen(reverse)-i] = *ptr;
    }
printf("%s", reverse);
return 0;
}

Thank you!

EDIT:
Sorry I mis-posted these as comments below

Thanks for the help, the first and second comment got it! I did have the required #includes, I just forgot to copy them into stack overflow. You were right, now I'm stuck on the non-null terminated strlen(). I'll solve that one on my own. Thanks again!

I spoke too soon, it compiled alright, but there is a logic error. The while loop will execute one time. However, subsequent loops still fail, regardless of the initial value of i. The line that causes the failure is the the line that calls realloc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何必那么矫情 2024-07-20 09:30:50

您无法重新分配最初未分配的内存。 您应该将反向声明为“char *”并对其进行malloc。

这会让你继续下去,但你真的应该考虑以块的形式重新分配,而不是一次一个字节。 哦,我没有费心去解决当你尝试 strlen 时“reverse”可能不是 null 终止的事实 - 我将把它作为练习留给读者。

int main(){
char  first[]="hello mark", strng='h';
char* reverse;
char* ptr=&first[10];
reverse = (char*)malloc(1);
reverse[0] = '\0';
int i=0;
    while(ptr > (&strng-0xf4240)){
        printf("%c", *ptr--);
        reverse = (char*)realloc(reverse, (i++ * sizeof(char)));
        reverse[strlen(reverse)-i] = *ptr;
    }
printf("%s", reverse);
return 0;
}

You can't realloc memory that wasn't malloced in the first place. You should declare reverse as "char *" and malloc it to start with.

This will get you going, but you really should think about reallocing in chunks, rather than one byte at a time. Oh, I didn't bother to fix the fact that "reverse" is probably not null terminated when you try the strlen - I'll leave that as an exercise for the reader.

int main(){
char  first[]="hello mark", strng='h';
char* reverse;
char* ptr=&first[10];
reverse = (char*)malloc(1);
reverse[0] = '\0';
int i=0;
    while(ptr > (&strng-0xf4240)){
        printf("%c", *ptr--);
        reverse = (char*)realloc(reverse, (i++ * sizeof(char)));
        reverse[strlen(reverse)-i] = *ptr;
    }
printf("%s", reverse);
return 0;
}
你曾走过我的故事 2024-07-20 09:30:50

或者:

  • 您正在使用以下命令编译 C 代码
    C++编译器。 这就是为什么它认为
    你需要转换返回的 void*
    通过realloc 到 char*。 这
    转换在 C 中是合法的。

或者

  • 您未能包含 stdlib.h,因此编译器认为 realloc 返回 int

编辑

不允许出现在分配的 LHS 上(不是“左值”)。 不过,很高兴您接受了另一个答案。 它有一些好的建议。

Either:

  • You are compiling your C code with a
    C++ compiler. That's why it thinks
    you need to cast the void* returned
    by realloc to a char*. The
    conversion would be legal in C.

or

  • You failed to include stdlib.h, so the compiler thinks realloc returns int

Edit

On reading your code again, the actual problem is that you are assigning to an array, which isn't allowed to appear on the LHS of an assignment (isn't an 'lvalue'). It's good that you accepted the other answer, though. It has some good advice.

万劫不复 2024-07-20 09:30:50

您无法重新分配最初未分配的内存。

是的,绝对正确,因为为了重新分配,malloc 必须拥有有关您操作的内存块的一些附加信息(通过块分配表,或存储在指针之前的 4 个字节中)。 但你仍然可以写:

char * ptr1 = malloc(16);
char * ptr2 = ptr1 + 8;
ptr2 = realloc(ptr2,32);

并且你不会有编译器错误。 不过,在运行时您仍然可能会遇到段错误。

的方式存在根本区别。

char * tab = "toto";

但是,C 编译器处理and

char tab[] = "toto";

在第一种情况下,tab 是一个 char * 类型的指针,并且是像任何普通变量一样的左值。 它可能大部分时间驻留在寄存器中,或者可能是堆上的地址。
在第二种情况下,tab 是一个数组,一个常量,因此不是左值。 它可能在程序集级别被替换为指向二进制可执行文件的文本部分的地址,其中存在字符串“toto”的数据。

You can't realloc memory that wasn't malloced in the first place.

Yes, definitely true, since in order to reallocate, malloc has to have have some additionnal information on the chunk of memory you manipulate (either through a block alocation table, or stored in the 4 bytes just before your pointer). But you could still write:

char * ptr1 = malloc(16);
char * ptr2 = ptr1 + 8;
ptr2 = realloc(ptr2,32);

And you would not have a compiler error. You would still probably get a segfault at run time, though.

However, there is a fundamental difference in the way a C compiler treats

char * tab = "toto";

and

char tab[] = "toto";

In the first case, tab is a pointer, of type char *, and is a lvalue like any normal variable. It probably resides in a register most of the time, or could be an address on the heap.
In the second case, tab is an array, a constant, and is thus not an lvalue. It is probably replaced at assembly level by an address that points to the text section of your binary executable, where the data for the string "toto" resides.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文