MaMu
MaMu

Reputation: 1869

Pointers - confusion

I'm wondering if it is always better to use pointers. I have a structure with only one byte (or some integers ). This structure contains i.e. parameters to a routine and will be passed there. If I don't use pointers (inside of the struct), but pass this struct by pointer, would the change (for logPrio) made in main_loop be visible outside (i.e in main)?

Please see this sample to get me:

typedef struct mainloop_param_t
{
  unsigned char *logPriop;
  //or versin w/o pointer 
  unsigned char logPrio;
}mainloop_param_t;

int main()
{
  mainloop_param_t mlparams;


  unsigned char logPrio;
  mlparams.logPriop = &logPrio;
  // or nothing cause mlparams.logPrio already initialized
  // would mlparams.logPrio be a copy or original?  

  g_timeout_add (5000, (GSourceFunc)main_loop, &mlparams);
}

Upvotes: 0

Views: 132

Answers (3)

Metalartis
Metalartis

Reputation: 115

If I don't use pointers (inside of the struct), but pass this struct by pointer, would the change (for logPrio) made in main_loop be visible outside (i.e in main)?

Yes, after you have passed the address of mlparams, every change made in main_loop can be visible in main, regardless of whether a pointer or an variable inside mlparams.

I guess your probloem is when a pointer should be used. In my opinion, pointer is just a tool to access data, it should not be a problem of itself. The important things are "which place the data should be?" and "how does programs access the data?"

The followings are parts of usages:

  1. data is here(locally) and will be accessed here --> just define and use an local variable
  2. data in somewhere(another scope, e.g. outside) and will be accessed here --> define a pointer that point to the data and access tha data via operator *
  3. data is here and will be accessed in somewhere(another scope, e.g. inside) --> define an variable and pass the address of the variable somewhere
  4. data in somewhere and will be accessed in otherwhere --> define a pointer that point to the data and pass the pointer otherwhere
  5. sometimes you need some data that may be in a large amout, insdead of copying all of them to stack(might cause overflow), it's better to copy an address of the beginning of the data. The copy of the address is just a pointer.

Upvotes: 1

Lundin
Lundin

Reputation: 213809

  unsigned char logPrio;
  mlparams.logPriop = &logPrio;

In this code, it doesn't make any sense to use a pointer. The change happens in main and will be visible to g_timeout_add since you pass this struct to that function.

I think you are confusing things with how parameters are passed by pointer to a function. If you have a function like

void func (int x)
{
  x = 2;
}

then the original variable passed to the function is not altered, because x inside the function is a copy of the variable from the caller.

int var = 1;
func(var);
printf("%d", var); // will print 1

If you want to alter the variable's value, you would have to write the function like:

void func (int* x)
{
  *x = 2;
}

int var = 1;
func(&var);
printf("%d", var); // will print 2

Upvotes: 0

haccks
haccks

Reputation: 106012

I'm wondering if it is always better to use pointers.

Use pointers when it is needed. Unnecessarily use of it may cause some confusion later to you and others readers.

If I don't use pointers, would the change (for logPrio) made in main_loop be visible outside (i.e in main)?

In case of global variable and structs YES, Otherwise NO.

Upvotes: 1

Related Questions