how to send pointer to a function?

in a link list program i wanna send the new node pointer to a insert func so it can insert it into the list of nodes.
i have some other functions too, like delete, create node, insert, navigate and ...
any one get and return pointers. i used this syntax but it's not working

1
2
3
4
5
person insert(person &cp,person &pp,person &sp)
{
    //work with cp,pp,sp
    return cp;
}


and i call the function like this:

insert(cp,pp,sp);
what should i do?

cp -> current node pointer
pp -> previews node p
ssp -> start node p
Last edited on
closed account (zb0S216C)
You can pass a pointer in two ways. The first way is a pointer-to-a-pointer. The second way is a reference to a pointer (more preferred).

Here's an example of a pointer-to-a-pointer:

1
2
3
4
void Function( int **ThisPointer )
{
    std::cout << **ThisPointer << std::endl;
}

This code dereferences the first parameter twice which reveals the value pointed to by the pointed-to pointer.

Here's an example of a reference-to-a-pointer:

1
2
3
4
void Function( int *&RefPointer )
{
    std::cout << *RefPointer << std::endl;
}

The first argument in this function refers to the address of the pointer.

Wazzak
tnx it works. and now how can i return a pointer from func to main?
closed account (zb0S216C)
Quite simply. All you have to do is place an asterisk before the function identifier and after the function return type-specifier. For example:

1
2
3
4
int *Function( )
{
    // ...
}

Note that you should never return a pointer that points to a variable/object that is local to the function from which the pointer is returning.

Wazzak
tnx for ur answer Wazzak.
now my sample prog is like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	void test(int *&p);
	int *a,b,*c;
	b=5;
	a=&b;
	c = test(a);
	cout<<*c;
	getchar();
}

int *test(int *&p)
{
	cout<<*p<<endl;
    int *q=new int;
    *q=5;
	return q;
}


and it fails on the eq operator on this line:
c = test(a);
the err is like this: " error C2440: '=' : cannot convert from 'void' to 'int *' "
closed account (zb0S216C)
I've noticed a few things that're easy to fix. Firstly, functions cannot be declared within a scope of another function. They need to be declared globally. For example:

1
2
3
4
5
6
7
8
9
10
11
void Function( ... );

int main( )
{
    // ...
}

void Function( ... )
{
    // ...
}

A function that omits the body during its declaration is called a Prototype Function. These are required in C++. I'll provide a link regarding this.

Secondly, a function that has a return type-specifier of void cannot return anything. However, if it was a void pointer, then that's OK. A void pointer basically means that it can point to anything but the data it's pointing to is unknown.

Thirdly, your function, test( ), is ambiguous. This means that the compiler cannot decide which version of test( ) to call.

References:
Prototype Functions: http://en.wikipedia.org/wiki/Function_prototype
void: http://msdn.microsoft.com/en-us/library/fxky5d0w(v=vs.80).aspx


Wazzak
Last edited on
tnx a lot Wazzak. :)
Topic archived. No new replies allowed.