-1

So I'm trying to change the value at x[2] from 0 to 8 using a method, the way I have this isn't working. How can I do this? I tried searching around but came to no avail.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

 void changevar(int* x){
    int* y;
    y = &x[2];
    y = 8;
 }
 int main(int argc, char** argv){
    int* c;
    c = (int*) malloc(sizeof(int));
    printf("here %d\n", c[2]);
    changevar(&c);
    printf("here %d\n", c[2]);
    free(c);
}

EDIT: I'm new to pointers

  • 2
    You allocate space for one integer, but access a third in a "buffer". The behavior of your code is undefined, there is no "fixing" it. – StoryTeller - Unslander Monica Feb 07 '17 at 18:58
  • 2
    1) `c[2]` is undefined behavior. 2) `changevar(&c)` is a constraint violation (i.e. non-compilable). `changevar` expects an `int *` argument. You are passing an `int **` argument. 3) `y = 8` is a constraint violation (i.e. non-compilable). You are not allowed to assign arbitrary integers to pointers. – AnT stands with Russia Feb 07 '17 at 18:59
  • @AnT I fixed that, thanks for pointing that out! I originally had the parameter has (int** x) for changevar. Do you know how I can change the of x[2] though? – reVolutionary Feb 07 '17 at 19:02
  • 1
    It is supposed to be `*y = 8`. Or simply `x[2] = 8` without any `y` at all. The previous points still stand though. – AnT stands with Russia Feb 07 '17 at 19:04
  • @AnT thank you! I just got into C programming and these pointers can get out of hand! Have been programming in python and such for years haha. – reVolutionary Feb 07 '17 at 19:05
  • you need to `malloc(sizeof(int) * 3);` at least if you want to access `c[2]` – yano Feb 07 '17 at 19:07
  • If your compiler shouts at you, you should listen. `y = 8;` is assigning and integer value to a pointer variable. At least you should get a warning. – Gerhardh Feb 07 '17 at 20:04

3 Answers3

1

You first need to allocate enough space:

c = malloc(3 * sizeof(int));

Notice that I didn't cast the return value.

The values are not initialized to zero. They can be anything ("undefined"). You can clear it with:

memset(c, 0, 3 * sizeof(int));

Next, you'll need to pass this value as is to your function. (It's already a pointer, after all.)

changevar(c);

Within your function, you'll need to dereference the address to access it:

*y = 8;

Those are the errors I see.

Community
  • 1
  • 1
chrisaycock
  • 36,470
  • 14
  • 88
  • 125
0

So there are two issues that are standing out to me.

  1. You're only allocating memory for one int in your malloc call but trying to access memory that would suggest you allocated a minimum space for 3 ints.
  2. Your function's parameter is supposed to be a int*, however with changevar(&c) you're passing in a int** because you giving the address of a pointer.

To fix these only a couple of changes need to be made...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

 void changevar(int* x){
    x[2] = 8; // <-- can be simplified to this one line
 }

 int main(int argc, char** argv){
    int* c;
    int amt = 3; // <-- number of ints you want to be able to have space for
    c = malloc(sizeof(int) * amt); // <-- multiply the size and the amount you want
    printf("here %d\n", c[2]);
    changevar(c); // <-- remove the '&' from the argument to just pass the pointer 'c'
    printf("here %d\n", c[2]);
    free(c);
}

The output becomes:

here 0
here 8
m_callens
  • 6,100
  • 8
  • 32
  • 54
0

Garbage values are being printed because size of array c is 1.

c = (int*) malloc(sizeof(int));

will create an array c of size 1.

c[2] is a garbage value. (In most systems, 0 is printed instead of a garbage number.)

Your &c[2] and y do not refer to the same address.

What you want to do is probably this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void changevar(int** x){
    *(*x+2)=8;
}
 int main(int argc, char** argv){
    int* c;
    c = (int*) malloc(3*sizeof(int));
    for(int i=0;i<3;i++){
        c[i]=0;
    }
    printf("here %d\n", c[2]);
    changevar(&c);
    printf("\nhere %d\n", c[2]);
    free(c);
}

Prints:

here 0
here 8
chrisaycock
  • 36,470
  • 14
  • 88
  • 125