Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

Char pointer Manupulation

When u declare a pointer to char and then in next line you initialize it then where it is stored?
Some times it gives Error and some times it works. Why it is so?

Ex.:
char *str;
str = "This in My Question";
 
0
terajiv
Asked:
terajiv
  • 10
  • 4
  • 4
  • +2
1 Solution
 
ScottyDawgCommented:
what you should declare is

char str [] = "This is my question";

char* str is just a pointer. It declares no memory.
0
 
rbrCommented:
To ScottyDaw: Wrong char str[] doesn't delcare any memory too.

To terajiv:
This should work but is a little bit dangerous. Many compiler/OS use this a const char * and readonly memory and you are not able to change it. Better use

str = strdup ("This is My Question");
0
 
rbrCommented:
To ScottyDaw: Sorry. Your code is right but it won't work if you use

char str[];

str = "This is my question";

But if you use strdup you can use char *str;

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ScottyDawgCommented:
Well I suppose the real question is do you need to allocate storage on the stack or the heap.

0
 
ScottyDawgCommented:
yes you can use strdup but the pointer returned has been malloc'd so remember you have to free it.
0
 
terajivAuthor Commented:
My Actual Question was about Storage of memory... Where exactly it is stored.... I think ScottyDawg is going near to the answer. but still I need clarification about storage of memory..
0
 
terajivAuthor Commented:
That statement is going to store the memory in stack it seems
0
 
rbrCommented:
char []="abdsbsb"; use the stack (i.g,)


char *str;
str=strdup("ananan"); use the heap (i.g.)
0
 
ozoCommented:
char str[]="abdsbsb";  is static storage
0
 
terajivAuthor Commented:
I got the Ans for 1st Que.
 Now can anybody pl. tell me why Some times it gives Error and some times it works?
0
 
rbrCommented:
can you post your full code, pls!
0
 
ozoCommented:
what error does it sometimes give?
0
 
LucHoltkampCommented:
If you code

double a_global = 3.1414926536
int f()
{
   char* str = "alkjsflkjsdfjasldjfsadlfjalsdjfljasdlfja"
  /* etc */
  strcat(str, "ouch!");   // will almost certainly fail
  strcpy(str, "ouch?");  // not garanteed to work
}

Then what happens in most compilers is that the character pointer str is stored on stack (it is an automatic) but the actual string ("alkjsflkjsdfjasldjfsadlfjalsdjfljasdlfja") is stored in a piece of memory the compiler reserves for statics (the global section). This is the same piece of memory that is used to store the double a_global.
This also means that the string exists during the whole lifetime of the program, although it is only is accessable in the function f.

Now, the exact way the compiler stores such strings is not part of the standard, that means, it is not garanteed that str can be treated as a regular piece of memory such as the heap, so any actions on the string that change the string are undefined. Thats the reason that in C++ this kind of definitions is preferably done as const char, although the upper definition is accepted for backwards compatibility issues:

const char* str = "this is ok"; // read only!!

By the way, notice that there is no difference between:

char *str  = "alksjflkasjfi";
char str[] =  "alksjflkasjfi";

these result in exacly the same assembler statements: str as auto and the string as global storage
---------------------------------------------------------------------------------------------
Two possible ways of defining storage for strings that is fully rewritable:

// This is my non-rewritable string in global storage
const char *my_string = "This is the string we want to be rewritable"

// allocate automatic stack storage for the string
char str[strlen(MY_STRING)+1];  // explicitly allocate stack storage
strcpy(str, MY_STRING);              // copy the string

// allocate heap storage for the string
char *str = strdup(MY_STRING); // from the heap, strdup allocates and copies
free(str);                                     // when you're done with the str

Luc
0
 
terajivAuthor Commented:
I got the Ans... Thanx for it...
Actually When u declare
Char *str;
and initialize it with
str = "This is my string";

then if memory is available in stack it will store it into stack if it is not available then it will give run time error...
0
 
ozoCommented:
char *str;
 str = "This is my string";
by itself should not give a run time error
But what are you then doing with that *str?
trying to write to that *str might give an error.
0
 
LucHoltkampCommented:
There is no difference between:

char *str = "An example";

or

char *str2;
str2 = "Another example";

Both result in exactly the same assembly statements.
(i.e. pointer str, str2 as automatic on stack (4 bytes), and the string as global storage.)

Luc
0
 
terajivAuthor Commented:
>>char *str;
>>str = "This is my string";

and
>>char *str = "An example";

are one and the same ur rt. Luc. But if there is no memory in stack it gives an error.
0
 
terajivAuthor Commented:
>>char *str;
>>str = "This is my string";

and
>>char *str = "An example";

are one and the same ur rt. Luc. But if there is no memory in stack it gives an error.
0
 
ozoCommented:
What do you mean if there is no memory in stack?  and what error does it give?
0
 
terajivAuthor Commented:
Imean if stack is full...then it gives run time memory referancing error....
0
 
LucHoltkampCommented:
If you're stack overflows all fails... not only the declaration, but also functioncalls, interrupts...
How do you know the stack overflows?
In any case, the string itself is not stored on stack.
Luc
0
 
terajivAuthor Commented:
Hi Luc,

I thought the string is stored on stack So Can u pl. tell me where it is stored when I declare as:
char *str= "My String";
Actually my first Que was that only. pl. clarify...
0
 
LucHoltkampCommented:
If you define

char *str = "My String";

The pointer str is stored on stack (4 bytes in win32) and the character "My String" is stored in global memory together with all the other statics and globals.
There are three different memomry locations a program uses:
stack (automatics), heap (malloc) and global (statics)
Luc
0
 
terajivAuthor Commented:
Thanx  LucHoltkamp
I was not knowing this...

0
 
terajivAuthor Commented:
Thanx  LucHoltkamp
I was not knowing this...

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 10
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now