Solved

Char pointer Manupulation

Posted on 2000-02-18
25
333 Views
Last Modified: 2010-04-15
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
Comment
Question by:terajiv
  • 10
  • 4
  • 4
  • +2
25 Comments
 
LVL 1

Accepted Solution

by:
ScottyDawg earned 50 total points
ID: 2534422
what you should declare is

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

char* str is just a pointer. It declares no memory.
0
 
LVL 10

Expert Comment

by:rbr
ID: 2534427
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
 
LVL 10

Expert Comment

by:rbr
ID: 2534431
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
 
LVL 1

Expert Comment

by:ScottyDawg
ID: 2534434
Well I suppose the real question is do you need to allocate storage on the stack or the heap.

0
 
LVL 1

Expert Comment

by:ScottyDawg
ID: 2534437
yes you can use strdup but the pointer returned has been malloc'd so remember you have to free it.
0
 
LVL 3

Author Comment

by:terajiv
ID: 2541556
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
 
LVL 3

Author Comment

by:terajiv
ID: 2541575
That statement is going to store the memory in stack it seems
0
 
LVL 10

Expert Comment

by:rbr
ID: 2542402
char []="abdsbsb"; use the stack (i.g,)


char *str;
str=strdup("ananan"); use the heap (i.g.)
0
 
LVL 84

Expert Comment

by:ozo
ID: 2542546
char str[]="abdsbsb";  is static storage
0
 
LVL 3

Author Comment

by:terajiv
ID: 2545101
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
 
LVL 10

Expert Comment

by:rbr
ID: 2545146
can you post your full code, pls!
0
 
LVL 84

Expert Comment

by:ozo
ID: 2545219
what error does it sometimes give?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 2546339
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
 
LVL 3

Author Comment

by:terajiv
ID: 2548918
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
 
LVL 84

Expert Comment

by:ozo
ID: 2548945
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
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 2549562
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
 
LVL 3

Author Comment

by:terajiv
ID: 2556400
>>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
 
LVL 3

Author Comment

by:terajiv
ID: 2556455
>>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
 
LVL 84

Expert Comment

by:ozo
ID: 2556527
What do you mean if there is no memory in stack?  and what error does it give?
0
 
LVL 3

Author Comment

by:terajiv
ID: 2556683
Imean if stack is full...then it gives run time memory referancing error....
0
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 2557440
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
 
LVL 3

Author Comment

by:terajiv
ID: 2560595
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
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 2564044
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
 
LVL 3

Author Comment

by:terajiv
ID: 2567389
Thanx  LucHoltkamp
I was not knowing this...

0
 
LVL 3

Author Comment

by:terajiv
ID: 2567495
Thanx  LucHoltkamp
I was not knowing this...

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now