-2

My question is in the title and is more of a syntax related question. Does anyone know what the * is doing in the function below? See here:

int* reat(int *n)
{
     int i, *array;
     do 
     { 
         printf("n="); scanf("%d", n);
     } while (*n < 1);
     array = (int *)malloc(*n * sizeof(int));
     for (i = 0; i < *n; i++)
     { 
        printf("%d. broj: ", i + 1);
        scanf("%d", array + i); 
     }
     return array;
}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 5
    Please be more clear in your question. Which particular asterisk are you talking about ? – nos Dec 28 '14 at 21:35
  • 1
    The function returns a variable of type `int*`. – barak manos Dec 28 '14 at 21:37
  • 1
    My favorite tutorial for pointers http://www.cprogramming.com/tutorial/c/lesson6.html – Brian Tracy Dec 28 '14 at 21:39
  • 1
    @barakmanos: No, the function returns a *value* of type `int*`. – Keith Thompson Dec 28 '14 at 21:59
  • possible duplicate of [How to understand complicated function declarations?](http://stackoverflow.com/questions/1448849/how-to-understand-complicated-function-declarations) – Kaz Dec 29 '14 at 01:56
  • @KeithThompson: Yep, bad sentence phrasing of mine, but enough for everyone to understand, because even though the caller gets an `int*` **value** in the stack (like you said), the function itself ends with `return` followed by an `int*` variable, thus "syntactically" returning an `int*` **variable**. – barak manos Dec 29 '14 at 07:53
  • The function could return any *value* of type `int*`, for example `return (int*)NULL;` (superfluous cast for clarity). Functions return values, not objects. In this case, it happens to return the value of an object, but that object doesn't even exist after the function returns. – Keith Thompson Dec 29 '14 at 07:58

2 Answers2

3

The * in int* reat(int *n) indicates in the return that this function is returning a pointer to an integer value rather than the integer value itself. The * indicates in the argument list that this function also wants a pointer to an integer value rather than a "raw" integer value for its argument.

For example,

int x = reat(n); // assume n is a pointer to an int

probably won't compile on most systems. If it does, you'll be storing a memory address in x rather than the integer value you were expecting. Instead write

int *x = reat(n)

to store in x the pointer (to some integer value) returned by reat(). Consider this function:

int addone(int x) {
  return 1 + x;
}

This function takes an integer value for its argument. To access the value of the integer pointed to by the return from reat() and use it in addone(), we'll need to call it like so:

int *x = reat(n)
addone(*x)

Thereby dereferencing x with the * operator to access the integer value it points to. addone(x) won't compile because x, without dereferencing, is a memory address and not an integer.

Understanding when the * is being used to define a pointer and when its being used to dereference one will become second nature over time. Trust that any time it shows up in a function definition, whether as an argument or a return, it indicates a pointer is being used.

Mike Goldin
  • 237
  • 1
  • 7
2

The syntax

int i, *array;

declares i to be a variable of type int and array to be a variable of type int*. This sort of declaration isn't particularly common, but is legal C code.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065