Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying array inside a function in C

// test.txt
50
13
124

-

void hi ( int *b, FILE *pfile ) {

    rewind ( pfile );
    int i;
    for( i = 0 ; i < 3 ; i++ ) {
         fscanf ( pfile, "%d", &b[i] );
    }
}

int main ( void ) {

    FILE *fp = fopen ( "test.txt", "r" );
    int a[10];  //putting extra size for test.

    hi ( &a[0], fp );   
    printf("%d,%d,%d\n", a[0], a[1], a[2]);

    fclose ( fp );
    return 0;
}

I'm trying to understand pointers when there's array involved. While I was testing the code above, I noticed that by putting a different index value such as hi ( &a[0], fp ) to hi ( &a[1], fp ) I get different results.

//result of [ hi ( &a[0], fp ) ] //result of [ hi ( &a[1], fp ) ] 50,13,124 junk#,50,13 .

I am really confused about the results because in the 'hi' function I specify the start of the array from i = 0 which should mean it's storing the value starting from a[0]. But it seems like putting 1 instead of 0 is somehow moving values aside. Why is this happening?

like image 988
ChewChew Avatar asked Aug 12 '17 23:08

ChewChew


People also ask

How do you change an array within a function?

If you've entered a single-cell array formula, select the cell, press F2, make your changes, and then press Ctrl+Shift+Enter..

How do you modify an existing array?

push() adds item(s) to the end of an array and changes the original array. unshift() adds an item(s) to the beginning of an array and changes the original array. splice() changes an array, by adding, removing and inserting elements. slice() copies a given part of an array and returns that copied part as a new array.

Can array be modified?

Modify array to another given array by replacing array elements with the sum of the array. Given an array input[] consisting only of 1s initially and an array target[] of size N, the task is to check if the array input[] can be converted to target[] by replacing input[i] with the sum of array elements in each step.

Can you append an array in C?

An array is a contiguous block of memory and if you want to append an element, you have to write it to the position following the last occupied position, provided the array is large enough.


2 Answers

You are not 'puuting a different size' but providing a pointer to the 1st element in the array and then the 2nd element.

Since in the second case you gave a pointer to the 2nd element, and tried to read 10 elements (starting at the 2nd position) but only have 9 valid elements when starting at the 2nd one, you are then reading into memory that is not in the array.

like image 184
lostbard Avatar answered Oct 15 '22 23:10

lostbard


This might make more sense for you if you think of & as an operator which acts on a single item, much like - acts on a number to change the sign. In this case, & takes the address of whatever you give it. In the case of &a[0], you are taking the address of a[0]. In the case of &a[1], you take the address of a[1]. In other words, you take the address of different elements of the array. So in the function, b contains the address that you passed to it. As far as the function knows, what you pass in is the "first" element of the array. It knows nothing of any of the preceding elements.

like image 6
Code-Apprentice Avatar answered Oct 16 '22 00:10

Code-Apprentice