473,511 Members | 14,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer to string

39 New Member
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
6 4876
Banfa
9,065 Recognized Expert Moderator Expert
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
jerico
39 New Member
ok.But what about the memory allocation for this program?

Jerico
Nov 4 '06 #3
horace1
1,510 Recognized Expert Top Contributor
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
jerico
39 New Member
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
horace1
1,510 Recognized Expert Top Contributor
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
jerico
39 New Member
Thanks.

Jerico
Nov 6 '06 #7

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

Similar topics

7
5162
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
14
2392
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
18
9053
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the...
2
35494
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
30
3565
by: ggnguser | last post by:
It's part of a test and I'm stumped. There is a function void foo(char **x) The function signature is given and cannot be changed, so no passing of other values. The test case involves...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.