tags:

views:

307

answers:

8

Hi,

This is a very simple question but what does the following function prototype mean?

int square( int y, size_t* x )

what dose the size_t* mean? I know size_t is a data type (int >=0). But how do I read the * attached to it? Is it a pointer to the memory location for x? In general I'm having trouble with this stuff, and if anybody could provide a handy reference, I'd appreciate it.

Thanks, Joshua


Thanks everybody. I understand what a pointer is, but I guess I have a hard hard time understanding the relationship between pointers and functions. When I see a function prototype defined as int sq(int x, int y), then it is perfectly clear to me what is going on. However, when I see something like int sq( int x, int* y), then I cannot--for the life of me--understand what the second parameter really means. On some level I understand it means "passing a pointer" but I don't understand things well enough to manipulate it on my own.

+2  A: 
tur1ng
+6  A: 

How about a tutorial on understanding pointers?

In this case however, the pointer is probably used to modify/return the value. In C, there are two basic mechanisms in which a function can return a value (please forgive the dumb example):

It can return the value directly:

float square_root( float x )
{
    if ( x >= 0 )
        return sqrt( x );
    return 0;
}

Or it can return by a pointer:

int square_root( float x, float* result )
{
    if ( x >= 0 )
    {
        *result = sqrt( result );
        return 1;
    }
    return 0;
}

The first one is called:

float a = square_root( 12.0 );

... while the latter:

float b;
square_root( 12.00, &b );

Note that the latter example will also allow you to check whether the value returned was real -- this mechanism is widely used in C libraries, where the return value of a function usually denotes success (or the lack of it) while the values themselves are returned via parameters.

Hence with the latter you could write:

float sqresult;
if ( !square_root( myvar, &sqresult ) )
{
   // signal error
}  
else
{ 
   // value is good, continue using sqresult!
}
Kornel Kisielewicz
+2  A: 
abc
On a side note, I've always found reason (4) to be an unfortunate (but sometimes unavoidable) usage of pointers in C based languages.
Kai
cout << *ptr + 1; is a C++ idiom, try printf("%d\n", *ptr + 1);
Andrew McGregor
+1  A: 

size_t *x means you are passing a pointer to a size_t 'instance'.

There are a couple of reasons you want to pass a pointer.

  1. So that the function can modify the caller's variable. C uses pass-by-value so that modifying a parameter inside a function does not modify the original variable.
  2. For performance reasons. If a parameter is a structure, pass-by-value means you have to copy the struct. If the struct is big enough this could cause a performance hit.
R Samuel Klatchko
A: 

There's a further interpretation given this is a parameter to a function.

When you use pointers (something*) in a function's argument and you pass a variable you are not passing a value, you are passing a reference (a "pointer") to a value. Any changes made to the variable inside the function are done to the variable to which it refers, i.e. the variable outside the function.

You still have to pass the correct type - there are two ways to do this; either use a pointer in the calling routine or use the & (addressof) operator.

I've just written this quickly to demonstrate:

#include <stdio.h>

void add(int one, int* two)
{
    *two += one;
}

int main()
{
    int x = 5;
    int y = 7;
    add(x,&y);

    printf("%d %d\n", x, y);

    return 0;
}

This is how things like scanf work.

Ninefingers
That's still passing by value, because you're passing a copy of a pointer to a function, not a reference.
Helper Method
Thanks everybody. I understand what a pointer is, but I guess I have a hard hard time understanding the relationship between pointers and functions. When I see a function prototype defined as int sq(int x, int y), then it is perfectly clear to me what is going on. However, when I see something like int sq( int x, int* y), then I cannot--for the life of me--understand what the second parameter really means. On some level I understand it means "passing a pointer" but I don't understand things well enough to manipulate it on my own.
Josh
+1  A: 

Pointer Basics

Pointers And Memory

Helper Method
A: 
int square( int y, size_t* x );

This declares a function that takes two arguments - an integer, and a pointer to unsigned (probably large) integer, and returns an integer.

size_t is unsigned integer type (usually a typedef) returned by sizeof() operator.

* (star) signals pointer type (e.g. int* ptr; makes ptr to be pointer to integer) when used in declarations (and casts), or dereference of a pointer when used at lvalue or rvalue (*ptr = 10; assigns ten to memory pointed to by ptr). It's just our luck that the same symbol is used for multiplication (Pascal, for example, uses ^ for pointers).

At the point of function declaration the names of the parameters (x and y here) don't really matter. You can define your function with different parameter names in the .c file. The caller of the function is only interested in the types and number of function parameters, and the return type.

When you define the function, the parameters now name local variables, whose values are assigned by the caller.

Pointer function parameters are used when passing objects by reference or as output parameters where you pass in a pointer to location where the function stores output value.

C is beautiful and simple language :)

Nikolai N Fetissov
A: 

In response to your last comment, I'll try and explain.

You know that variables hold a value, and the type of the variable tells you what kind of values it can hold. So an int type variable can hold an integer number that falls within a certain range. If I declare a function like:

int sq(int x);

...then that means that the sq function needs you to supply a value which is an integer number, and it will return a value that is also an integer number.

If a variable is declared with a pointer type, it means that the value of that variable itself is "the location of another variable". So an int * type variable can hold as its value, "the location of another variable, and that other variable has int type". Then we can extend that to functions:

int sqp(int * x);

That means that the sqp function needs to you to supply a value which is itself the location of an int type variable. That means I could call it like so:

int p;
int q;

p = sqp(&q);

(&q just means "give me the location of q, not its value"). Within sqp, I could use that pointer like this:

int sqp(int * x)
{
    *x = 10;
    return 20;
}

(*x means "act on the variable at the location given by x, not x itself").

caf