473,480 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pointer discussion

Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr='A';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr='A';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry
Jul 22 '05 #1
11 1227
Harry wrote:
Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr='A';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr='A';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry


void foo(int i)
{
i = 1;
}

void foo(int *i)
{
*i = 1;
}

Do you see the difference?

--
Regards,
Slava

Jul 22 '05 #2
You can simplify your problem by replacing char* with int:

void funcA(int* i)
{
*i = 10;
}

void funcB(int i)
{
i = 20;
}

int main()
{
int i = 0;
funcA(&i);
funcB(i);
return 0;
}

The call to funcA changes 'i' in main, just like your first call
works as expected. The call to funcB, however, doesn't change 'i'
in main at all (and in your case causes a runtime error when you
dereference 'ch', a null pointer). Why? Because all funcB is
doing is altering a copy of 'i', just like your second program is
only alterning a copy of 'ch' in your second func.

(Also note that main should return an int, not void, despite what
your compiler may accept.)

Harry wrote:
Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr='A';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr='A';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry

Jul 22 '05 #3
In your second example you are sending func a null-ptr with you reassign to
point to your newly created char. After assinging the pointer area a 'A' you
assign your local parameter pointer a copy of that pointer but this is and
will not be propagated back to your calling function. From main you still
have the local null-ptr as you assigned it. You then dereference your
null-ptr and that has undefined behaviour.

--
Regards // Bo
"Harry" <ar******@fht-esslingen.de> wrote in message
news:f9**************************@posting.google.c om...
Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr='A';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr='A';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry

Jul 22 '05 #4
Derek wrote:
You can simplify your problem by replacing char* with int:

void funcA(int* i)
{
*i = 10;
}

void funcB(int i)
{
i = 20;
}

int main()
{
int i = 0;
funcA(&i);
funcB(i);
return 0;
}

The call to funcA changes 'i' in main, just like your first call
works as expected. The call to funcB, however, doesn't change 'i'
in main at all (and in your case causes a runtime error when you
dereference 'ch', a null pointer). Why? Because all funcB is
doing is altering a copy of 'i', just like your second program is
only alterning a copy of 'ch' in your second func.

(Also note that main should return an int, not void, despite what
your compiler may accept.)

Harry wrote:
Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr='A';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr='A';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry

I might be wrong , but I think you both guys not quite correct.
Your example with funcB is not the same as the original one with char
*ch passed to the function. Look at the differences.

void funcB(int i)
{
i = 20;
}

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr='A';
ch=ptr;
}

In first case the funcB will not return anything an any sence, while in
the original example func(char *ch) writes to the address passed to the
fucntion, hence the value will be kept and can used whan function
func(char *ch) returns.

In the original posting I think
there's not much difference between code, one with pointer to pointer is
just a bit more complex, the same way you can use ***p and doesn't
make weather only brings inconvenience in this case.

Examples with ints are good but they are just a little different.

Crash me if I'm wrong.:)
Andrey.
Jul 22 '05 #5
Harry wrote:
Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr='A';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr='A';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry

The code breaks, because you call a function and pass a Zero pointer,
but a copy of a pointer is created in the function! You allocate mermory
, changing the address of this pointer, but it will no return back from
the function. When yo return you'll have a zero pointer as befoe and a
lost chunk of memory, because the pointer on the actual memory block is
destroyed. That's actually a serious error, which can be hard to trace.
I'd avoid allocating memory in the function, just because of the danger
to loose a pointer on the memory block.
Andrey.
Jul 22 '05 #6
Andrey wrote:
Derek wrote:
You can simplify your problem by replacing char* with int:

void funcA(int* i)
{
*i = 10;
}

void funcB(int i)
{
i = 20;
}

int main()
{
int i = 0;
funcA(&i);
funcB(i);
return 0;
}

The call to funcA changes 'i' in main, just like your first call
works as expected. The call to funcB, however, doesn't change 'i'
in main at all (and in your case causes a runtime error when you
dereference 'ch', a null pointer). Why? Because all funcB is
doing is altering a copy of 'i', just like your second program is
only alterning a copy of 'ch' in your second func.

(Also note that main should return an int, not void, despite what
your compiler may accept.)

Harry wrote:
Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr='A';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr='A';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry


I might be wrong , but I think you both guys not quite correct.
Your example with funcB is not the same as the original one with char
*ch passed to the function. Look at the differences.

void funcB(int i)
{
i = 20;
}

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr='A';
ch=ptr;
}

In first case the funcB will not return anything an any sence, while in
the original example func(char *ch) writes to the address passed to the
fucntion, hence the value will be kept and can used whan function
func(char *ch) returns.

In the original posting I think
there's not much difference between code, one with pointer to pointer is
just a bit more complex, the same way you can use ***p and doesn't
make weather only brings inconvenience in this case.

Examples with ints are good but they are just a little different.

Crash me if I'm wrong.:)
Andrey.

Disregard my statement. Because the memory allocated in the function the
function changes the pointer and doesn't return it actually, since the
copy of a pointer is created.
Andrey.
Jul 22 '05 #7

"Vyacheslav Kononenko" <vy********@NOkononenkoSPAM.net> wrote in message
news:A7*****************@mencken.net.nih.gov...
Harry wrote:
Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr='A';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr='A';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry


void foo(int i)
{
i = 1;
}

void foo(int *i)
{
*i = 1;
}


And if you (OP) want to avoid using the cumbersome pointer syntax, you can
pass by reference using &.

void foo(int& i)
{
i = 1;
}
Jul 22 '05 #8
Andrey <st*******@yahoo.ca> wrote in message news:<vQtod.303296$Pl.125487@pd7tw1no>...
Harry wrote:
Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr='A';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr='A';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry

The code breaks, because you call a function and pass a Zero pointer,
but a copy of a pointer is created in the function! You allocate mermory
, changing the address of this pointer, but it will no return back from
the function. When yo return you'll have a zero pointer as befoe and a
lost chunk of memory, because the pointer on the actual memory block is
destroyed. That's actually a serious error, which can be hard to trace.
I'd avoid allocating memory in the function, just because of the danger
to loose a pointer on the memory block.
Andrey.


Well..I feel that the example with funcB() was not totally correct.
But it has given me an insight. Though I am passing a pointer and
trying to store the starting address of the dynamically allocated
char, still my 2nd code amounts to pass by value. I always thought
that passing a pointer always means that I am passing by reference.
But the second code shows that I was wrong. It means that if I have to
pass by reference, I need to pass the ADDRESS of the object which
should finally hold something, and which I can later use in my main()
or somewhere else. My 1st code does exactly that.

Am I speaking logically?? Pls send ur suggestions to make this concept
more clear.

with regards
Harry
Jul 22 '05 #9
Harry wrote:
[skip]
Well..I feel that the example with funcB() was not totally correct.
But it has given me an insight. Though I am passing a pointer and
trying to store the starting address of the dynamically allocated
char, still my 2nd code amounts to pass by value. I always thought
that passing a pointer always means that I am passing by reference.
But the second code shows that I was wrong. It means that if I have to
pass by reference, I need to pass the ADDRESS of the object which
should finally hold something, and which I can later use in my main()
or somewhere else. My 1st code does exactly that.

Am I speaking logically?? Pls send ur suggestions to make this concept
more clear.

with regards
Harry

Just look at this again:

void foo(sometype i)
{
i = value;
}

void foo(sometype *i)
{
*i = value;
}

sometype can be int from my sample or typedef to char* it works exactly
the same.
--
Regards,
Slava

Jul 22 '05 #10
> I always thought
that passing a pointer always means that I am passing by reference.
But the second code shows that I was wrong. It means that if I have to
pass by reference, I need to pass the ADDRESS of the object which
should finally hold something, and which I can later use in my main()
or somewhere else. My 1st code does exactly that.


If you want to modify an object, you must either have a reference to it
or its address. Let's talk about addresses.

void f(char c);

Event without any more information on f(), we are able to say that it is
impossible for f() to modify the original variable passed to it since
a copy is made.

Now, if you use a pointer, you get the original variable's address and
you are able to modify it. To do that, you must dereference that pointer :

void f(char *c)
{
*c = 'A';
}

int main()
{
char c;

f(&c);
}

Here, I assume the variable you want is of type char. Let's say you
want it to be a pointer to a char :

char *c;

Let's rewrite main() for starting :

int main()
{
char *c;

f(&c);
}

Remember, for f() to be able to modify the original variable, you must
pass its address and that's exactly what we're doing, no matter what the
variable's type is. So what's the type of the address of a pointer to a
char? It is a pointer to a pointer to a char :

void f(char **c);
*So to have a pointer on a variable, you need to add a level of
indirection, whatever the type is.*

Original type Pointer to that type
int int*
char char*
int* int**
char* char**
char*** char****

That's the only way of modifying the original with pointers : you must
get its address.

Passing by reference makes it simpler :

void f(char *&c)
{
// remember : c here is an alias for c in main()
// these two names are *exactly* the same

c = new char[10];
}

int main()
{
char *c = 0;

f(c);

// now c points to a 10 char long buffer.

delete c;
}

Jonathan
Jul 22 '05 #11
"Harry" <ar******@fht-esslingen.de> wrote in message
news:f9**************************@posting.google.c om...
Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr='A';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr='A';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry


In the first version, you pass in a pointer to ch. Thus, when you
dereference this pointer and do the assignment, you're changing the original
ch. In the second version, you're passing ch itself, so what you end up
modifying is a local copy of the pointer, not the pointer itself. When you
return from func, the original ch is unchanged (and NULL), so when you
attempt to do the printf, the program crashes (its behaviour is undefined).

HTH,
Stuart
Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
4940
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
69
7363
by: Ken | last post by:
Hi all. When referring to a null pointer constant in C++, is there any reason to prefer using 0 over a macro called NULL that is defined to be 0? Thanks! Ken
25
9173
by: MC | last post by:
Hi I have the following piece of code. int *p; p = malloc(10); int *j; j = p; free(p); Is this legal ? will j still point to the same
44
5750
by: Nicolas | last post by:
On most implementations of the standard C library, the functions that copy characters in a previously allocated buffer, like strcpy or strcat, are returning the pointer to that buffer. On my...
16
3349
by: jacob navia | last post by:
Valid pointers have two states. Either empty (NULL), or filled with an address that must be at a valid address. Valid addresses are: 1) The current global context. The first byte of the data...
35
2851
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
204
12880
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
51
4389
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
3249
by: Michael | last post by:
Hi all, why do I get a message: warning: passing arg 1 of `collectInput' from incompatible pointer type In 'main' have: char inputString; collectInput(&inputString);
3
1160
by: a_agaga | last post by:
I will start a new discussion about this issue here. The question popped up after investigating an other issue:...
0
7048
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6911
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
4787
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.