英文:
variable changing its value on its own in c
问题
以下是您提供的代码的翻译部分:
#include <stdio.h>
#include <string.h>
int main() {
printf("Hello World\n");
int uninitialisedArrayOfInt[4];
int initialisedArrayOfInt[] = {0, 1, 2, 3};
int len = sizeof(uninitialisedArrayOfInt)/sizeof(int);
for (int i = 0; i < len; i++){
uninitialisedArrayOfInt[i] = i+1;
initialisedArrayOfInt[i] = i+2;
}
int i = 0;
while(i<len){
printf("%i\n", uninitialisedArrayOfInt[i]);
i++;
}
int arr[1][1];
char str1[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '#include <stdio.h>
#include <string.h>
int main() {
printf("Hello World\n");
int uninitialisedArrayOfInt[4];
int initialisedArrayOfInt[] = {0, 1, 2, 3};
int len = sizeof(uninitialisedArrayOfInt)/sizeof(int);
for (int i = 0; i < len; i++){
uninitialisedArrayOfInt[i] = i+1;
initialisedArrayOfInt[i] = i+2;
}
int i = 0;
while(i<len){
printf("%i\n", uninitialisedArrayOfInt[i]);
i++;
}
int arr[1][1];
char str1[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};
printf("%s\n", str1);
char str2[] = "Hello World!";
printf("%s\n", str2);
int len2 = strlen(str1);
printf("%i\n", len2);
strcat(str1, str2); // 从这一步到下一步,len值发生了变化
printf("%s\n", str1); // len变成了1919899424
char copycat[sizeof(str2)];
strcpy(copycat, str2);
printf("%s\n", copycat);
int* lenPtr = &len;
printf("%p\n", lenPtr);
printf("%i\n", *lenPtr);
*lenPtr = 200;
printf("%i\n", len);
}
'};
printf("%s\n", str1);
char str2[] = "Hello World!";
printf("%s\n", str2);
int len2 = strlen(str1);
printf("%i\n", len2);
strcat(str1, str2); // 从这一步到下一步,len值发生了变化
printf("%s\n", str1); // len变成了1919899424
char copycat[sizeof(str2)];
strcpy(copycat, str2);
printf("%s\n", copycat);
int* lenPtr = &len;
printf("%p\n", lenPtr);
printf("%i\n", *lenPtr);
*lenPtr = 200;
printf("%i\n", len);
}
请注意,您提供的代码中,strcat()
函数会将 str1
和 str2
连接起来,这可能导致 str1
的长度增加,但它不应该影响 len
的值。如果 len
的值在 strcat()
后发生变化,这可能是由于某种编译器或调试器的特殊行为或问题引起的。在标准情况下,strcat()
不应该更改 len
的值。
英文:
For context, I'm a beginner in C and I was making a file with all the important details of the language, and I noticed that a variable (type int
) that I defined in the start seemed to change its value later on in the program when I accessed it using pointers.
here's how the code goes on (I removed all the comments that explained each line(bc Im a beginner))
#include <stdio.h>
#include <string.h>
int main() {
printf("Hello World\n");
int uninitialisedArrayOfInt[4];
int initialisedArrayOfInt[] = {0, 1, 2, 3};
int len = sizeof(uninitialisedArrayOfInt)/sizeof(int);
for (int i = 0; i < len; i++){
uninitialisedArrayOfInt[i] = i+1;
initialisedArrayOfInt[i] = i+2;
}
int i = 0;
while(i<len){
printf("%i\n", uninitialisedArrayOfInt[i]);
i++;
}
int arr[1][1];
char str1[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '#include <stdio.h>
#include <string.h>
int main() {
printf("Hello World\n");
int uninitialisedArrayOfInt[4];
int initialisedArrayOfInt[] = {0, 1, 2, 3};
int len = sizeof(uninitialisedArrayOfInt)/sizeof(int);
for (int i = 0; i < len; i++){
uninitialisedArrayOfInt[i] = i+1;
initialisedArrayOfInt[i] = i+2;
}
int i = 0;
while(i<len){
printf("%i\n", uninitialisedArrayOfInt[i]);
i++;
}
int arr[1][1];
char str1[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};
printf("%s\n", str1);
char str2[] = "Hello World!";
printf("%s\n", str2);
int len2 = strlen(str1);
printf("%i\n", len2);
strcat(str1, str2); //the len values changes from this step to the next
printf("%s\n", str1); //len becomes 1919899424
char copycat[sizeof(str2)];
strcpy(copycat, str2);
printf("%s\n", copycat);
int* lenPtr = &len;
printf("%p\n", lenPtr);
printf("%i\n", *lenPtr);
*lenPtr = 200;
printf("%i\n", len);
}
'};
printf("%s\n", str1);
char str2[] = "Hello World!";
printf("%s\n", str2);
int len2 = strlen(str1);
printf("%i\n", len2);
strcat(str1, str2); //the len values changes from this step to the next
printf("%s\n", str1); //len becomes 1919899424
char copycat[sizeof(str2)];
strcpy(copycat, str2);
printf("%s\n", copycat);
int* lenPtr = &len;
printf("%p\n", lenPtr);
printf("%i\n", *lenPtr);
*lenPtr = 200;
printf("%i\n", len);
}
The funny part here is that I play around with len
only in the start and end and yet the value changes to 1919899424
(it changes) on the line where I concatenate str1
and str2
.
Just so you guys know I ran this code in vscode and the compiler and debugger I am using is gcc and gdb.
I had to include this entire code because without it the error does not occur
as many are confused the strcat()
function does not change the value of len
as explained here (https://www.codecademy.com/courses/learn-c-arrays-and-strings/lessons/strings-c/exercises/concatenating-strings#:~:text=What%20actually%20happens%20is%20the%20function%20takes%20the%20two%20strings%20and%20creates%20a%20char%20array%20of%20size%20strlen(src)%20%2B%20strlen(dst)%20%2B%201.)
答案1
得分: 2
str1
中没有足够的空间来连接其他字符串。在执行 concat
后,您修改了 str1
数组之外的内存,并改变了 len
变量的值(在这种情况下)。
英文:
There is no free space in str1
to concat other string. After concat
you modify memory outside of str1
array and change the value of len
variable (in this case).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论