Question:
What is a star between variable and name in C?
World Dominator M
2013-10-07 13:06:37 UTC
Im supposed to implement the following function:

void minAndMax( int a[], int n, int *largest, int *smallest);

When passed an array of length n, the function should store the minimum number in the array in smallest and the maximum number in largest. But what does those stars mean? Are they pointers or something?
Three answers:
Empire539
2013-10-07 14:01:07 UTC
Yes, those are pointers.



In this type of situation, most likely the function uses pointer parameters in order to make use of "parameter passing". It's a technique which allows you to save values of variables even after the function has finished executing. Essentially, it's kind of like letting a function return multiple values at the same time.



For example, let's say you had something like this:



void doStuff(int a, int b) {

    a++;

    b--;

}



And let's say you wrote the following somewhere:

int x = 5;

int y = 10;

doStuff(x, y);



In this case, if you print out the values of x and y, you'll still get 5 and 10. This is because the doStuff only takes a copy of the value and works with the modified values locally.



Now if you had something like this:

void doStuff2(int* a, int* b) {

    (*a)++; /* (*a) means dereference a, i.e. "take the pointer a and get its value" */

    (*b)--;

}



int x = 5;

int y = 10;

doStuff2(&x, &y);



Now, you're passing the address of x and y into the function, i.e. the function will operate with the values at those specific memory locations. If you printed out x and y, you would get x = 6 and y = 9.
Black S
2013-10-07 20:56:26 UTC
Hi,



The asterisk means the value is a memory address or pointer to that variable. In the minAndMax routine, the routine is probably going to modify those values (largest and smallest). Since the values are being passed to the routine on the stack (a memory place that is erased when the subroutine returns), the routine needs a way to return these values. By passing addresses/pointers to the routine, the routine can directly change the variables in the code section that called the subroutine.



If you can see the minAndMax routine, you can probably see some code that shows:



*largest = someHighValue;

or

*smallest = someLowValue;



In C/C++, pointers are a fundamental part of the language. To be worth your salt, know how adding/subtracting values from pointers affects their values as well as gain some insights into the types of variables being used in C/C++. (Example:



int * ip;

char * cp;



ip++; // if memory address was 2000, now it is 2004 (if int size is 4)

cp++;// if memory address was 3000, now it is 3001 (if char size is 1)

ip += 2; // if memory address was 2004, now it is 2012 (if int size is 4)
Andy T
2013-10-07 21:44:37 UTC
Pointer, why do you need it? Nope, or Yes depending how you look at the language, it was designed on a fridge-mini back in the days where almost anything must be saved to minimum use. The equivalent in Java would have to be done with a "proper" OOP-based wrapper and that would be too bloated for such computers about the equal to NES, and we were already talking about a relatively lean platform today.



You have the right ideal of instead of copying but that's how this function elect to do its returns.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...