0

Am I correct to say that if I want to change the content of a variable in a structure I have to give a pointer to the structure, using a different function to change the struction, like this:

Struct:

typedef struct data{
    int row;
    int column; 
}data;

the var in the struct changer function:

struct data* init_maze(void) {

    data information;      //init information struct
    data *infoPoint;       //int information struct pointer

    int row = 6;
    int column = 10;

    infoPoint->row = row;   //not working but should be updating information
    infoPoint->column = column; //same as above

    return infoPoint;
}

But this is not working as intended. The code breaks and nothing happens. Could anyone please explain what I am doing wrong.

Kipt Scriddy
  • 743
  • 2
  • 14
  • 22
  • There's no info about `struct maze`, only `struct data`. You haven't shown what `mazePointer` points to, etc... –  Feb 23 '13 at 17:28
  • 3
    For a script kiddie you did a poor job copy-pasting stuff. – cnicutar Feb 23 '13 at 17:29
  • @H2CO3 Sorry messed up with replacing my variables – Kipt Scriddy Feb 23 '13 at 17:39
  • 2
    Looking [here](http://stackoverflow.com/q/15038193/771663), it seems to me that you didn't quite understand dynamic memory and pointers. As it is for an assignment, I suggest to review carefully these concepts instead of modifying code at random. Just my 2 cents. – Massimiliano Feb 23 '13 at 17:47
  • 1
    I agree with @Massimiliano. Maybe this will help: http://www.cprogramming.com/tutorial/c/lesson6.html – Benjamin Maurer Feb 23 '13 at 17:56
  • None of this code makes any sense. – Lundin Sep 05 '17 at 09:18

4 Answers4

0

infoPoint should be declared as:

data* infoPoint = malloc (sizeof(data));

Then this code should work:

infoPoint->row = row;   //not working but should be updating information
infoPoint->column = column; //same as above



And function init_maze doesn't need struct prefix:

data* init_maze(void) 


data information;  

is also unused

  • Does this means I need to allocate memory for this data in the way of "malloc (sizeof(data))" so that when I initialize "data" it gets the required memory? – Kipt Scriddy Feb 23 '13 at 17:38
  • Sorry just had to edit it because I edited my question using different variable names – Kipt Scriddy Feb 23 '13 at 17:42
0

Ask yourself first, what you want to accomplish here.

For example, you declare "data information" and you never use it. You don't even point "infoPoint" to anything before you manipulate it. Since you wrote nothing of a "SEGFAULT", I guess you are omitting some code?

Anyway. Do you want to create a new "data" struct and return it with some initial values? Then you have to either: A) allocate some memory and return a pointer to this piece of memory on the heap (which must be allocated with malloc (or similar) and deallocated with free at some point"

B) You can take a pointer to a struct data as parameter and put your values in there.

data *init_maza(void) {
   data *infoPoint = malloc(sizeof(struct data));

   data->row = 6;
   data->column = 10;

   return data;
}

void init_maze(data *outp) {
   outp->row = 6;
   data->column = 10;
}

You can call B) with a pointer to a struct data on the stack or heap:

data foo; data *bar = malloc(sizeof(struct data));

// Initialize init_maze(&foo); init_maze(bar);

Note the ampersand '&', the 'address-of' operator.

You can also use a pointer to pointer parameter so you still can return something else, like an error code. Used very often:

int init_maze(data **d) {
    *data = malloc(sizeof(struct data));
    (*data)->row = 6;
    (*data)->column = 10;

    return 0;
}
Benjamin Maurer
  • 3,602
  • 5
  • 28
  • 49
0

Your sample code is trying to modify an uninitialized pointer to a struct, so infoPoint->row = row; is an invalid operation since infoPoint is not initialized.

You could change your init function like this:

void init_maze(data *infoPoint) {
    infoPoint->row = 6;
    infoPoint->column = 10;
}

Then use it like this:

int main()
{
    data information;
    init_maze(&information);
}

data information; will allocate your struct on the stack, and you can pass a pointer to it for init_maze to do its job.

mikhail
  • 5,019
  • 2
  • 34
  • 47
0

Another answer covers using pointers properly, but for different approach, you can pass structs around just like simple variables, so consider this version:

struct data init_maze(void) {

    data information;   
    information.row = 6;   
    information.column = 10;

    return information;
}

This is not as efficient as passing pointers around, but for most practical purposes and in most cases difference is irrelevant. And here size of this struct is two integers, which puts it solidly in this category.

hyde
  • 60,639
  • 21
  • 115
  • 176