-3

I want to pass a pointer to a function. Lets say I am creating a structure and declare a pointer to that structure. I allocate memory to that pointer and then I want to "pass" that pointer to a function so I can do stuff to that block of memory. How I do that?

2 Answers2

1

It is done the same way as passing a normal variable. You shouldn't think of a pointer any different than a variable, as a pointer is a variable (although a bit special).

Say you have an integer and a pointer to that integer:

int a = 0;
int * ptr = &a;

Note that ptr and a are both variables, and in this instance, we use the reference operator & to reference a by &a which gives the address of a -- this is a pointer to a.

Now say you have two functions:

void foo(int i){}
void bar(int * p){} 

Now these two functions do the same thing (nothing), but one takes an int and one takes a pointer to an int, int *. If you called foo(a) and modified i, i would be modified in the function body, but have no effect on a. If you called bar(ptr) and modified the value pointed to by ptr by using the dereference operator * as something like *ptr = 1, it would modify the value pointed to by ptr, which in this case would be a.

In your case, passing a struct would be just the same. Say you have a structure

struct MyStruct { int b; };

And had two more functions

void structFoo(struct MyStruct m) {}
void structBar(struct MyStruct * mp) {}

You could work on the pointer in the same way as our above int and int * functions, although you would now also have to access the struct components by dereferencing the struct with (*mp).b, or the shorthand version mp->b.

For ease of use, you may also want to do something like

typedef struct MyStruct MyStruct;

Which will allow you to use MyStruct as the type name rather than requiring struct MyStruct as in the above functions, although you may want to avoid doing this until you fully understand what is happening and get through difficulties with pointers.

Hopefully that gives you some idea of what is going on, although the C++ tutorial on pointers gives a much more in-depth discussion than what I can provide.

It would be a good exercise to create some small programs and mess around with pointer and regular types while printing the results to help understand what is happening where. You should remember that to allocate a pointer, you're going to need to use either new or malloc(), otherwise you will have a SEGFAULT error. This is not an issue when referencing a variable declared as I have done with &a above as the memory is already allocated when a declaration such as int a is made. A declaration of a pointer allocates the memory for that pointer, but not the memory that it points to. This eventually leads into the difference between the stack and the heap, although I wouldn't think about that until you have a very thorough understanding of pointers.

Daniel Underwood
  • 2,191
  • 2
  • 22
  • 48
  • @Aristotelis No problem! Just play around with pointers and stuff some. Don't get too bothered if you run into errors or something unexpected -- just think of it as a learning experience! The main thing that you should take away from this is that pointers are just fancy variables to directly access memory. When working with languages such as Java, nearly everything is a pointer even though that part is hidden from you. – Daniel Underwood May 28 '15 at 17:06
0

Structure in c/cpp is like all the other types. You can use & and * as you do it for int.

towelenee
  • 11
  • 1
  • 1
  • The way we use `*` (and, to some degree, `&`) on `int` is entirely different. Besides, nothing about this tells the OP how to "pass" a pointer into a function, as requested. – Lightness Races in Orbit May 28 '15 at 16:46