1

It seems there are many questions of the form "should I declare X?" but not this specific one. I hope it is ok to ask this.

The title says it all: why should I declare a pointer? Even better: there are risks if I do not declare the pointer? Consider the following examples:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <string.h>

void func(int *ptr);

int main (int argc, char **argv)
{

  int a;
  int *PTRa;

  a = -1;
  PTRa = &a;


  func(PTRa);
  printf("%d\n", a);

  return 0;

}

void func(int *ptr)
{
  *ptr = 1;
  return;
}

I get a=1. In this case I would say the pointer is declared (and assigned as well): I have the line int *PTRa; (declaration) and the line PTRa = &a; (assignment). The results is correct. I don't get any warning.

Imagine now to replace the main with the following:

int main (int argc, char **argv)
{

  int a;

  a = -1;

  func(&a);
  printf("%d\n", a);

  return 0;

}

Here I do not declare the pointer but just give the address of a to func. The result is correct and I don't get warnings.

My understanding is that the two approaches are identical: func always gets the same input, the address of a. I would even dare to say that I feel the second approach to be better, as I feel it to be clearer and I feel the variable PTRa to be useless and somewhat redundant. However, I always see codes where the first approach is used and I have the feeling I will be told to do so. Why?

GRquanti
  • 527
  • 8
  • 23
  • 1
    Both versions have exactly the same effect, and will probably be compiled the same with optimizations. Whether you want the temporary variable is just a matter of which way you feel is more clear to read. – Nate Eldredge Mar 06 '21 at 19:19
  • Q: why should I declare a pointer? A: You shouldn't declare *ANYTHING* unless you *NEED* it. 1) `void func(int *ptr)` is silly: it should probably be `int func(int i)`, and return an int. It *DEFINITELY* shouldn't have a (superfluous) "return". 2) declaring `PTRa` just so you can use it as a parameter to "func()" is silly. Your second example, where you just call `func(&a);` is much better. – paulsm4 Mar 06 '21 at 19:26
  • But Nate Eldredge is 100% correct: `Whether you want the temporary variable is just a matter of which way you feel is more clear to read.` There is no "one true answer". Ultimately, it's just a matter of "preference". – paulsm4 Mar 06 '21 at 19:27

3 Answers3

1

In this case there's no benefit in creating a separate pointer variable.

It might be necessary in more complex cases, just like it's sometimes necessary to create variables of any other type.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
1

You are correct: there's no point in declaring a pointer in your example. A pointer is just a variable that holds an address. The cleaner approach is to pass directly the address of the variable: func(&a) instead of doing one extra step and declaring PTRa.

Note that not all cases are this simple. For example, if you want to have an array of ints, but you want to be able to grow that array dynamically because you don't know how big it should be you have to declare a pointer:

int count = ...; // get the count from the user, from a file, etc
int *list_of_ints = malloc(sizeof(int) * count);
if (list_of_ints == NULL)
{
    // malloc failed.
    printf("Not enough memory!\n");
    exit(1);
}

// Now `list_of_ints` has enough space to store exactly `count` `int`s

EDIT: as @paulsm4 pointed out in a comment, the question Why use pointers? is a great source of information related to this topic.

EDIT 2: one good reason to want a pointer to the address of a variable might be that you want a pointer inside a structure or array:

struct foo
{
    int x;
};

struct bar
{
    int y;
    struct foo f;
};

struct bar b;
struct foo *ptr_foo = &b.f;

You can now work more easily with b.f because you're just working with a struct foo.

icebp
  • 1,608
  • 1
  • 14
  • 24
  • 1
    Thank you for the answer. I am aware of `malloc` and how it works. My focus was specifically on pointers to a single address. – GRquanti Mar 06 '21 at 19:29
  • 1
    @GRquanti Then let's say you're making a linked list. You'll need pointers to nodes. – HolyBlackCat Mar 06 '21 at 19:40
  • 1
    Nice! I am not aware of what a linked list is. I'll look at it. Thank you! – GRquanti Mar 06 '21 at 19:41
  • 1
    @GRquanti: I didn't realize your question was really that basic: "Q: What are pointers, and when/why should I use them?" A: I encourage you to look at the replies in this thread: [Why use pointers?](https://stackoverflow.com/a/162988/421195) – paulsm4 Mar 06 '21 at 20:47
  • 1
    @paulsm4 Oh, but I am aware of what pointers are and why to use them. I know the example I posted is not a good example of when a pointer should be used. You were correctly noting that in this case a function returning int works and is thus better. The example was just to express the syntax. The question, if you want, is "why people declare and assign pointers to a single address?". My current understanding is "there is no reason and a more compact syntax (as `func(&a)`) is probably better", as I originally suspected. – GRquanti Mar 06 '21 at 23:32
  • 2
    You're correct, In general, "there's no reason...and the more compact syntax is probably better". HOWEVER: this isn't an Absolute Rule. Like Nate Eldredge said (it's worth repeating): there are often very good reasons to create temp variables. It's entirely a matter of preference. Please *DO* read the responses in the [thread](https://stackoverflow.com/questions/162941/why-use-pointers/162988#162988). They're enlightening :) – paulsm4 Mar 07 '21 at 02:50
  • 1
    As another example you may want or need a pointer inside and array or a structure. I added an example in my answer. – icebp Mar 07 '21 at 08:14
0

From the title, I thought you're talking about pointer type, but actually, you are asking if declaring a variable is needed.

  • Variable is a piece of memory, storing some numbers(bytes), and the type of the variable, indicating how you and your program interpret those bytes: integer? float? character? etc.
  • Pointer is the memory address, it could be of a variable, or a function, or something else.
  • A variable of pointer is a small area in the memory, storing the address of other(or even same) memory.
  • You decide if you need an extra variable to store the pointer. It's the same to the decision that if you want a variable to store an integer:
int v = -1;
abs(v); // use variable
abs(-1); // use constant
James Lan
  • 248
  • 2
  • 8
  • I'm getting confused. My understanding is that pointer is a type, not a memory address. The type pointer stores a memory address, in the same way `int` stores a natural signed number and so on. A memory address, on the other hand, I would not say it is a type but rather a value. A value telling where the memory cell is, in the same way in `int a=-1` the `-1` is a value telling how much `a` amounts to. – GRquanti Mar 06 '21 at 19:46
  • @GRquanti That's correct. `int *` is your pointer type, `PTRa` is your variable, whose type is `int *`. `&a` is the constant. – James Lan Mar 06 '21 at 22:44
  • Pointer stores address bytes, similar to int stores integer and char stores character as bytes in the memory. – James Lan Mar 06 '21 at 22:53