变量在C中自行更改其值。

huangapple go评论63阅读模式
英文:

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() 函数会将 str1str2 连接起来,这可能导致 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 &lt;stdio.h&gt;
#include &lt;string.h&gt;

int main() {

    printf(&quot;Hello World\n&quot;);

    int uninitialisedArrayOfInt[4];

    int initialisedArrayOfInt[] = {0, 1, 2, 3};


    int len = sizeof(uninitialisedArrayOfInt)/sizeof(int);


    for (int i = 0; i &lt; len; i++){
        uninitialisedArrayOfInt[i] = i+1;
        initialisedArrayOfInt[i] = i+2;
    }



    int i = 0;

    while(i&lt;len){
        printf(&quot;%i\n&quot;, uninitialisedArrayOfInt[i]);
        i++;
    }


    int arr[1][1];


    char str1[] = {&#39;H&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;, &#39; &#39;, &#39;W&#39;, &#39;o&#39;, &#39;r&#39;, &#39;l&#39;, &#39;d&#39;, &#39;!&#39;, &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
int main() {
printf(&quot;Hello World\n&quot;);
int uninitialisedArrayOfInt[4];
int initialisedArrayOfInt[] = {0, 1, 2, 3};
int len = sizeof(uninitialisedArrayOfInt)/sizeof(int);
for (int i = 0; i &lt; len; i++){
uninitialisedArrayOfInt[i] = i+1;
initialisedArrayOfInt[i] = i+2;
}
int i = 0;
while(i&lt;len){
printf(&quot;%i\n&quot;, uninitialisedArrayOfInt[i]);
i++;
}
int arr[1][1];
char str1[] = {&#39;H&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;, &#39; &#39;, &#39;W&#39;, &#39;o&#39;, &#39;r&#39;, &#39;l&#39;, &#39;d&#39;, &#39;!&#39;, &#39;\0&#39;}; 
printf(&quot;%s\n&quot;, str1);
char str2[] = &quot;Hello World!&quot;;
printf(&quot;%s\n&quot;, str2);
int len2 = strlen(str1);
printf(&quot;%i\n&quot;, len2);
strcat(str1, str2); //the len values changes from this step to the next
printf(&quot;%s\n&quot;, str1); //len becomes 1919899424
char copycat[sizeof(str2)];
strcpy(copycat, str2);
printf(&quot;%s\n&quot;, copycat);
int* lenPtr = &amp;len;
printf(&quot;%p\n&quot;, lenPtr);
printf(&quot;%i\n&quot;, *lenPtr);
*lenPtr = 200;
printf(&quot;%i\n&quot;, len);
}
&#39;}; printf(&quot;%s\n&quot;, str1); char str2[] = &quot;Hello World!&quot;; printf(&quot;%s\n&quot;, str2); int len2 = strlen(str1); printf(&quot;%i\n&quot;, len2); strcat(str1, str2); //the len values changes from this step to the next printf(&quot;%s\n&quot;, str1); //len becomes 1919899424 char copycat[sizeof(str2)]; strcpy(copycat, str2); printf(&quot;%s\n&quot;, copycat); int* lenPtr = &amp;len; printf(&quot;%p\n&quot;, lenPtr); printf(&quot;%i\n&quot;, *lenPtr); *lenPtr = 200; printf(&quot;%i\n&quot;, 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).

huangapple
  • 本文由 发表于 2023年6月15日 19:52:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定