Note that there are some explanatory texts on larger screens.

plurals
  1. POSame structure objects memory overlap?
    primarykey
    data
    text
    <pre><code>struct integer { int len; char* str; int* arr; }int1, int2; int main(void) { printf("Please enter 1st number\n"); int1.str= str_input(); int1.len=chars_read-1; int1.arr= func2(int1.len, int1.str); printf(("\%c\n"), *int1.str); printf("Please enter 2nd number\n"); int2.str = str_input(); int2.len=chars_read-1; printf(("\n %c\n"), *int1.str ); int2.arr= func2(int2.len, int2.str); </code></pre> <p>if the input is 4363 and 78596 , the output is 4 and 7 respectively. </p> <p>The output is not 4 and 4. Given that both are different objects, shouldn't both have different memory allocation? </p> <p>Please note: this is NOT a typographical error. I have used the same *int1.str both times. the problem is that although I have made no changes in it, its value is changing. How? I do not think that str_input() can make a difference. </p> <pre><code>char* str_input(void) { char cur_char; char* input_ptr = (char*)malloc(LINE_LEN * sizeof(char)); char input_string[LINE_LEN]; //while ((cur_char = getchar()) != '\n' &amp;&amp; cur_char&lt;='9' &amp;&amp; cur_char&gt;='0' &amp;&amp; chars_read &lt; 10000) for(chars_read=1; chars_read&lt;10000; chars_read++) { scanf("%c", &amp;cur_char); if(cur_char!='\n' &amp;&amp; cur_char&lt;='9' &amp;&amp; cur_char&gt;='0') { input_string[chars_read-1]= cur_char; printf("%c\n", input_string[chars_read-1]); } else{ break; } } input_string[chars_read] = '\n'; input_ptr = &amp;input_string[0]; /* sets pointer to address of 0th index */ return input_ptr; } //chars_read is a global variable. </code></pre> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload