Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer-to-pointer to char

Reply
Thread Tools

pointer-to-pointer to char

 
 
Jack
Guest
Posts: n/a
 
      05-28-2006
The following code can be compiled. But When I run it, it causes
"Segmentation fault".

int main(){
char **c1;

*c1 = "HOW"; // LINE1

++(*c1);
*c1 = "ARE";

++(*c1);
*c1 = "YOU";
}

c1 is a pointer-to-pointer to char. How to initialize c1 to three
strings: "HOW", "ARE", "YOU"?

Thanks a lot.

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-28-2006
Jack wrote:
> The following code can be compiled. But When I run it, it causes
> "Segmentation fault".
>
> int main(){
> char **c1;
>
> *c1 = "HOW"; // LINE1
>

c1 doesn't point to anything...
>
> c1 is a pointer-to-pointer to char. How to initialize c1 to three
> strings: "HOW", "ARE", "YOU"?
>

char *c1[] = { "HOW", "ARE", "YOU" };

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-28-2006
Ian Collins wrote:
> Jack wrote:
>
>>The following code can be compiled. But When I run it, it causes
>>"Segmentation fault".
>>
>>int main(){
>> char **c1;
>>
>> *c1 = "HOW"; // LINE1
>>

>
> c1 doesn't point to anything...
>
>>c1 is a pointer-to-pointer to char. How to initialize c1 to three
>>strings: "HOW", "ARE", "YOU"?
>>

>
> char *c1[] = { "HOW", "ARE", "YOU" };
>

I should have said

const char *c1[] = { "HOW", "ARE", "YOU" };

--
Ian Collins.
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      05-28-2006
Jack wrote:
> The following code can be compiled. But When I run it, it causes
> "Segmentation fault".
>
> int main(){
> char **c1;
>
> *c1 = "HOW"; // LINE1


Boom! Crash and burn! c1 is not initialized and *c1 is garbage.
>
> ++(*c1);
> *c1 = "ARE";
>
> ++(*c1);
> *c1 = "YOU";
> }
>
> c1 is a pointer-to-pointer to char. How to initialize c1 to three
> strings: "HOW", "ARE", "YOU"?
>
> Thanks a lot.
>

#include <stdlib.h>

char **c1 = malloc(3 * sizeof *c1);
c1[0] = "HOW";
c1[1] = "ARE";
c1[2] = "YOU";

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      05-28-2006

> char **c1;
>
> *c1 = "HOW";



Most (if not all) novices make this mistake at some stage, but it doesn't
take long before you can spot it from a mile away. Here's a sample:

char *p;

The variable "p" stores a memory address (at which resides a char). You
didn't initialise it, so it contains white noise. Let's say its value is:
8374534.

*p = 'a';


Now you dereference this pointer value, and write to the memory. So
you're writing 'a' to the memory location: 8374534.

You have to give "p" a legitimate value:

char k;

char *p = &k;


Let's say that k is stored at the memory address: 234873. Now p is
storing the value: 234873. This is a legitimate memory location to
access:

*p = 'e';


The crux of the matter is this: When you define a pointer variable,
you're DON'T actually allocate memory for the variable which it points
to. But it's easist to think along the following lines: A "char*" stores
a memory address.


-Tomás
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      05-28-2006
Tomás wrote:
>> char **c1;
>>
>> *c1 = "HOW";

>
>
> Most (if not all) novices make this mistake at some stage, but it doesn't
> take long before you can spot it from a mile away. Here's a sample:
>
> char *p;
>
> The variable "p" stores a memory address (at which resides a char). You
> didn't initialise it, so it contains white noise. Let's say its value is:
> 8374534.
>
> *p = 'a';


The value of an unitialized pointer is of no interest. The above
statement is undefined behavior and may cause segment fault or other error.
>
>
> Now you dereference this pointer value, and write to the memory. So
> you're writing 'a' to the memory location: 8374534.
>

Please see my comment above.

> You have to give "p" a legitimate value:
>
> char k;
>
> char *p = &k;
>

That's better. p holds the address of k and k is sufficient to hold 'e'.
>
> Let's say that k is stored at the memory address: 234873. Now p is
> storing the value: 234873. This is a legitimate memory location to
> access:
>

Again, the actual value of a pointer is of no interest.

> *p = 'e';
>

This works and now (k == 'e') is true.
>
> The crux of the matter is this: When you define a pointer variable,
> you're DON'T actually allocate memory for the variable which it points
> to. But it's easist to think along the following lines: A "char*" stores
> a memory address.
>

More correctly, an object of type char* may contain the address of an
object of type char.
>
> -Tomás



--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      05-28-2006
Joe Wright posted:


>> The variable "p" stores a memory address (at which resides a char).
>> You didn't initialise it, so it contains white noise. Let's say its
>> value is: 8374534.
>>
>> *p = 'a';

>
> The value of an unitialized pointer is of no interest.



It is for the purpose of my tutorial -- it gives the original poster a
concrete, down-to-earth example of what's going on. When you don't
initialise a variable, it sort of has a random value (which may not even
be a valid bit-pattern for the given type).


> The above
> statement is undefined behavior and may cause segment fault or other
> error.



Congratulations, you understand.


> That's better. p holds the address of k and k is sufficient to hold
> 'e'.



Should I presume that the remainder of your post simply repeats every
point I made in my previous post?


> This works and now (k == 'e') is true.



An inaccurate method of testing. I bet you I could get the following to
work on my system:

char *p;

*p = 'e';

if ( *p == 'e' ) NaivelyIndicateSuccess();


>> The crux of the matter is this: When you define a pointer variable,
>> you're DON'T actually allocate memory for the variable which it
>> points to. But it's easist to think along the following lines: A
>> "char*" stores a memory address.
>>

> More correctly, an object of type char* may contain the address of an
> object of type char.



(Maybe I'm getting mixed up with C and C++ here, but in C++, you can use
a char* to access ANY memory. Would you have to use an unsigned char* in
C?)


-Tomás
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      05-28-2006
Tomás wrote:
> Joe Wright posted:
>
>
>>> The variable "p" stores a memory address (at which resides a char).
>>> You didn't initialise it, so it contains white noise. Let's say its
>>> value is: 8374534.
>>>
>>> *p = 'a';

>> The value of an unitialized pointer is of no interest.

>
>
> It is for the purpose of my tutorial -- it gives the original poster a
> concrete, down-to-earth example of what's going on. When you don't
> initialise a variable, it sort of has a random value (which may not even
> be a valid bit-pattern for the given type).
>
>
>> The above
>> statement is undefined behavior and may cause segment fault or other
>> error.

>
>
> Congratulations, you understand.
>
>
>> That's better. p holds the address of k and k is sufficient to hold
>> 'e'.

>
>
> Should I presume that the remainder of your post simply repeats every
> point I made in my previous post?
>
>
>> This works and now (k == 'e') is true.

>
>
> An inaccurate method of testing. I bet you I could get the following to
> work on my system:
>
> char *p;
>
> *p = 'e';
>
> if ( *p == 'e' ) NaivelyIndicateSuccess();
>

But we agree that this is undefined, don't we?
>
>>> The crux of the matter is this: When you define a pointer variable,
>>> you're DON'T actually allocate memory for the variable which it
>>> points to. But it's easist to think along the following lines: A
>>> "char*" stores a memory address.
>>>

>> More correctly, an object of type char* may contain the address of an
>> object of type char.

>
>
> (Maybe I'm getting mixed up with C and C++ here, but in C++, you can use
> a char* to access ANY memory. Would you have to use an unsigned char* in
> C?)
>

Yes, 'unsigned char*' is the method to access any byte in memory in C.
Since C89 we have 'void*' to hold any memory address but it can't be
used to access memory directly.
>
> -Tomás



--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      05-28-2006
Joe Wright posted:


>> char *p;
>>
>> *p = 'e';
>>
>> if ( *p == 'e' ) NaivelyIndicateSuccess();
>>

> But we agree that this is undefined, don't we?



Yes of course. (I didn't want to state the obvious).


-Tomás
 
Reply With Quote
 
Jack
Guest
Posts: n/a
 
      05-28-2006
Thanks a lot.
But why the following code works well?
int *p;
*p = 5;

Moreover, I find that many textbooks use the following code to
initialize a char pointer:
char *c;
c = "test";

Is it wrong?

Thanks.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments