By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

pointer to string

P: 39
Hi.We have the following program..

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void fun(char*);
int main()
{
char *t="hello";
fun(t);
printf("%s",t);
return 0;
}
void fun(char *r)
{
r=(char*)malloc(15);
strcpy(r,"world");
printf("%s",r);
}

Here though we have passed the address of t and modifying its value in the function fun(),then why the change is not reflected?Please explain how different types of memory are used here?

Jerico
Nov 3 '06 #1
Share this Question
Share on Google+
6 Replies


Banfa
Expert Mod 5K+
P: 8,916
Here though we have passed the address of t
You have not passed the address of t. The address of t is

&t

you are passing

t

So you are passing t as am absolute value.

You are passing the address of the string literal "Hello" because this is what t contains.
Nov 3 '06 #2

P: 39
ok.But what about the memory allocation for this program?

Jerico
Nov 4 '06 #3

Expert 100+
P: 1,510
when you malloc() inside function fun() you don't change the value of the pointer t in the main() program - to do so you need to pass the address of t (i.e. &t) and the fun() function header then becomes
void fun(char **r)
where the parameter r is a pointer to char *

other changes are then required in fun() to dereferrence parameter r correctly - the resultant program is
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. void fun(char**);
  5. int main()
  6. {
  7. char *t="hello";
  8. fun(&t);
  9. printf("%s",t);
  10. return 0;
  11. }
  12. // function parameter is a pointer to char *
  13. void fun(char **r)
  14. {   
  15. *r=(char*)malloc(15);
  16. strcpy(*r,"world");
  17. printf("%s",*r);
  18. }
  19.  
when run it now gives
worldworld
Nov 4 '06 #4

P: 39
Thanks.but I have two question-

1.Isn't the string in the main() a constant string?Why are we not getting any eror here even sfter modifying it?
2.Can you give me the sequence in which various addresses are pushed and popped out from memory?

Jerico
Nov 5 '06 #5

Expert 100+
P: 1,510
Thanks.but I have two question-

1.Isn't the string in the main() a constant string?Why are we not getting any eror here even sfter modifying it?
2.Can you give me the sequence in which various addresses are pushed and popped out from memory?

Jerico
in the statement
char *t="hello";
the string "hello" is constant, t is a char * (pointer to char)

to make t constant you would have
const char *t="hello";

and you would get a compiler message on the call
fun(&t);
because fun() could change the value of t

program with some explaination and which prints addresses as well as the strings
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. void fun(char**);
  5. int main()
  6. {
  7. // define t as char* (pointer to char) and point it at "hello" 
  8. char *t="hello";     
  9. // print the address in t and what points at, i.e. "hello"
  10. printf("t contains address %x which contains %s\n", t, t);
  11. // call fun() passing the address of t   
  12. fun(&t);
  13. // print the address in t and what points at, i.e. "hello"
  14. printf("t contains address %x which  contains  %s\n", t, t);
  15. return 0;
  16. }
  17. // function parameter is a pointer to char *
  18. void fun(char **r)
  19. {   
  20. // malloc storage and assign address to char* r points to 
  21. *r=(char*)malloc(15);
  22. printf("malloc storage is at address %x \n", *r);
  23. // copy "world" into storage pointed at by r
  24. strcpy(*r,"world");
  25. printf("*r contains address %x which  contains  %s\n",*r, *r);
  26. }
  27.  
  28.  
a run gave
t contains address 404000 which contains hello
malloc storage is at address 3e5e50
*r contains address 3e5e50 which contains world
t contains address 3e5e50 which contains world
Nov 5 '06 #6

P: 39
Thanks.

Jerico
Nov 6 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.