pointer question (pretty basic)...

C

Chris Mantoulidis

I never liked pointers really much, so I decided to stay away from
them for a while. I know they're useful, so now I decided to actually
learn how the work, use them, etc.

Here's my question...

char *a = "some text";
char *b = "some other text";

Why will this work? Needn't I initialize the pointers first? (malloc
in C, new in C++, whatever)

And second question. Let's say I got a function that returns char *
("odd" if the parameter is odd, and "even" if the parameter is even)

char *s1 = CHAR_PTR_FUNCTION(1);
char *s2 = CHAR_PTR_FUNCTION(2);

The return value (returned like 'return "odd"' or 'return "even"'),
where is it stored? It has to be stored somewhere cuz otherwise *s1
would have the same value as *s2...

I'm getting confused here...

Any help?
 
M

Mark A. Odell

(e-mail address removed) (Chris Mantoulidis) wrote in

I never liked pointers really much, so I decided to stay away from
them for a while. I know they're useful, so now I decided to actually
learn how the work, use them, etc.

They are extremely powerful and are integral to the C language, I suggest
you become proficient in using them.
Here's my question...

char *a = "some text";
char *b = "some other text";

Why will this work? Needn't I initialize the pointers first?

You just did. You are allocating two pointers and them initializing them
to point to two strings stored somewhere else. Just don't try to write to
these pointers. In this case, you'd have been better to use const char
*pThing instead of char *pThing.
And second question. Let's say I got a function that returns char *
("odd" if the parameter is odd, and "even" if the parameter is even)

char *s1 = CHAR_PTR_FUNCTION(1);
char *s2 = CHAR_PTR_FUNCTION(2);

The return value (returned like 'return "odd"' or 'return "even"'),
where is it stored? It has to be stored somewhere cuz otherwise *s1
would have the same value as *s2...

The strings "odd" and "even" are stored somewhere that you don't need to
care about. Returning a point to a string of this type is save and you can
safely set your s1 and s2 pointer to point to them via the function
return. Pointer s1 and s2 may point to the same string in memory if both
numbers are even or odd, so what? As long as you don't try to write to the
strings pointed to by s1 or s2 you're fine.
 
D

Dan Pop

In said:
I never liked pointers really much, so I decided to stay away from
them for a while.

Without pointers, C is practically useless. Even the canonical "hello
world" program is using a pointer.
Here's my question...

char *a = "some text";
char *b = "some other text";

Why will this work? Needn't I initialize the pointers first? (malloc
in C, new in C++, whatever)

And second question. Let's say I got a function that returns char *
("odd" if the parameter is odd, and "even" if the parameter is even)

char *s1 = CHAR_PTR_FUNCTION(1);
char *s2 = CHAR_PTR_FUNCTION(2);

The return value (returned like 'return "odd"' or 'return "even"'),
where is it stored? It has to be stored somewhere cuz otherwise *s1
would have the same value as *s2...

I'm getting confused here...

Open your C book and read about string literals. You'll find the answer
to both your questions.

Dan
 
C

Chris Mantoulidis

Mark A. Odell said:
(e-mail address removed) (Chris Mantoulidis) wrote in



They are extremely powerful and are integral to the C language, I suggest
you become proficient in using them.


You just did. You are allocating two pointers and them initializing them
to point to two strings stored somewhere else. Just don't try to write to
these pointers. In this case, you'd have been better to use const char
*pThing instead of char *pThing.

The strings "odd" and "even" are stored somewhere that you don't need to
care about. Returning a point to a string of this type is save and you can
safely set your s1 and s2 pointer to point to them via the function
return. Pointer s1 and s2 may point to the same string in memory if both
numbers are even or odd, so what? As long as you don't try to write to the
strings pointed to by s1 or s2 you're fine.

Thanks, it's clear at last =P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top