0

I'm trying to learn C, so while making some exercises I ran into a problem I can't understand.

I created a character array buf [100][100], in it I store some strings. Now i would like to change a certain string in to another one by using the strcpy command. But when trying to adress it with a integer (z) it does not want to work. however when i manually adress the array with a value it works as intended.

can anyone give me an explanation for this?

Thank you in advance, Sam

code :

int main() {
    char buf [100][100];
    int i=0,j=0;
    int lijn;
    int x,k;
    int z;

    char stringN [100];
    FILE * fpointer = fopen("employees.txt","r");

    if (!fpointer) { return 1; }
    printf("regel nummer?:");
    scanf("%d",z);
    printf("nieuwe regel");
    scanf("%s",stringN);

    while(fgets(buf[i],100,fpointer)!=NULL){
        i++;
        j++;
    }


    strcpy(buf[z], stringN);
    strcat(buf[z], "\n");
    x=0;

    for (k=j;k>=1;k--){
        printf("%s",buf[x]);
        x++;
    }

    return 0;
}
Alexander Perechnev
  • 2,797
  • 3
  • 21
  • 35
Sam
  • 1
  • 1
  • There's no point in posting code which as you say works. Post the code which doesn't work, and it'd be better a complete program. – Armali Jan 08 '20 at 10:58

1 Answers1

0
    scanf("%d",z);

is wrong. You have to pass the address of the variable, i. e.

    scanf("%d", &z);
Armali
  • 18,255
  • 14
  • 57
  • 171
  • 1
    Thanks, that did it. But can you tell me why I need to "save" it to the adress instead of the variable? Intuitively saving it to the address of the variable or saving it to the variable results in the same if you are going to use the value of that variable. Especially since it is not necessary in the fget() command. – Sam Jan 08 '20 at 12:52
  • If you give `z` to `scanf`, the (undefined) _value_ of the variable is passed. But `scanf` needs to know *where* it has to store the converted integer, i. e. the _address_ of the variable. Good point asking about `fgets()`: Here (as well as with `scanf("%s", stringN)`) it is really the same, it's just so that an _array_ when passed to a function is _implicitly_ converted to its address. – Armali Jan 08 '20 at 13:12
  • aha! so i conclude that when you want to print from or store to an array you can index it with a variable. but when you want to do some fancy function with it like strcpy, strcat, etc you have to index with the address of that variable. Thanks for the help! – Sam Jan 08 '20 at 13:28
  • This conclusion sounds wrong. Passing the address of a variable is only adequate where an address is needed, especially when some value is to be stored by the called function, and especially not when you _index_. This has not necessarily something to do with _to print from or store to an array you can index_. Your compiler might have some options like `-Wall` to aid. – Armali Jan 08 '20 at 13:39
  • 1
    I will have to do some reading on the subject. But thanks again for all the help! – Sam Jan 08 '20 at 13:59