Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / general c++ question :)

Author
Message
Lightwar siX
20
Years of Service
User Offline
Joined: 24th Jun 2004
Location:
Posted: 9th Jul 2004 11:14
Hello !

Ive looked all over google for an answer to this and believe it or not i also have 2 full books on C++ that dont seem to cover the problem im having. So now i have resorted to bothering you people!

Anyways the question is i have something like this

char * ptr_Result=0;
char char_array[] = "foobar";
ptr_Result = &char_array;

but then the compiler throws this error

error C2440: '=' : cannot convert from 'char (*)[5]' to 'char *'

i dont get it... i dont see why its even looking at a character when its passing the address of the char array to the pointer?

Bah
Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 9th Jul 2004 11:21
You need to get the pointer to the first index of the array:

ptr_Result = &char_array[0];

That will point to the very first value in the array, so when you dereference the pointer you will ge "f", to get the rest of the array you must increase the pointer:

ptr_Result++;

Dereference that and you will get "o", that's the way pointers work with arrays.


"Computers are useless they can only give you answers."
Lightwar siX
20
Years of Service
User Offline
Joined: 24th Jun 2004
Location:
Posted: 9th Jul 2004 11:23
Ah thank you so much

Bah
Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 9th Jul 2004 11:27
Ahh, I take that back, it looks like it will print the whole array and not just the first char. Lol, I never knew that.


"Computers are useless they can only give you answers."
Lightwar siX
20
Years of Service
User Offline
Joined: 24th Jun 2004
Location:
Posted: 9th Jul 2004 11:37
Yeah i just assumed i was doing something wrong but now im really confused lol

Bah
TKF15H
21
Years of Service
User Offline
Joined: 20th Jul 2003
Location: Rio de Janeiro
Posted: 9th Jul 2004 11:55
or you could just do this:
ptr_Result = char_array;

An array is the same as a pointer in C.

Login to post a reply

Server time is: 2024-09-22 06:24:11
Your offset time is: 2024-09-22 06:24:11