473,321 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

pointer to string

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
6 4857
Banfa
9,065 Expert Mod 8TB
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
ok.But what about the memory allocation for this program?

Jerico
Nov 4 '06 #3
horace1
1,510 Expert 1GB
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
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 Expert 1GB
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
Thanks.

Jerico
Nov 6 '06 #7

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

Similar topics

7
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
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
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
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
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.