473,396 Members | 1,877 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,396 software developers and data experts.

Silly Pointer Problem

21
Hi ,

I just wanna know why such a wierd output is seen for the following program.I know it is pretty simple but i can't make it.Just a clue could help me to sort out the issue.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdio.h>
  3.  
  4. void func(char *s)
  5. {
  6.    printf("%x \n",s);
  7.    s="senthil";
  8.    printf("%s %x\n",s,s);
  9. }
  10.  
  11. int main()
  12. {
  13.    char* s ="manikandan";
  14.    printf("%s %x\n",s,s);
  15.    func(s);
  16.    cout<<s;
  17. }
zcarh0s5-135> !.
./a.out
manikandan 4eb0
4eb0 >>>>>>>>>> Same address is passed to func()
senthil 4ea0 >>>>>Here the address is diff !!!
manikandan


Can't we change the value of a string using char pointer.
Jan 29 '07 #1
4 1246
Banfa
9,065 Expert Mod 8TB
Can't we change the value of a string using char pointer.
No.

You initialised a pointer to an address in memory
You printed the contents of that address and the address itself
In the function
You print the address again
You change the value of the pointer passed to the function to a different address in memory
You printed the contents of the new address and the new address itself

Note that the function changes nothing in main, the original variable s still points to the original address.
Jan 29 '07 #2
horace1
1,510 Expert 1GB
you need to use strcpy() or strncpy() so
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdio.h>
  3.  
  4. void func(char *s)
  5. {
  6. printf("%x \n",s);
  7. strcpy(s,"senthil");
  8. printf("%s %x\n",s,s);
  9.  
  10. }
  11. int main()
  12. {
  13. char   s[] ="manikandan";
  14. printf("%s %x\n",s,s);
  15. func(s);
  16. cout<<s;
  17. cin.get();
  18. }
  19.  
note that you have to define an array in the calling program of sufficent size to receive the charcaters
Jan 29 '07 #3
willakawill
1,646 1GB
Can't we change the value of a string using char pointer.
Yes if you do it this way
Expand|Select|Wrap|Line Numbers
  1. void func(char *s)
  2. {
  3.     printf("%x \n",s);
  4.     strcpy(s, "senthil\0");
  5.     printf("%s %x\n",s,s);
  6.  
  7. }
  8. int main()
  9. {
  10.     char s[50] ="manikandan\0";
  11.     printf("%s %x\n",s,s);
  12.     func(s);
  13.     cout<<s;
  14.     getchar();
  15.     return 0;
  16. }
Jan 29 '07 #4
LSB
8
I would suggest to run your code in debugger: you will see what your pointers are and what data they represent.
Very, very useful !!!
Jan 29 '07 #5

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

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
13
by: Gernot Frisch | last post by:
Hi, assume this situation: class primitive { virtual void IsPointInside() =NULL; }; class sphere : public primitive { ... };
2
by: Bit byte | last post by:
What's wrong with this: //file1.h Class A { public: A(){i=1;} ~A(){;} void doThis(void){;} void doThat(void){;}
6
by: Joe Van Dyk | last post by:
#include <queue> #include <string> #include <iostream> using namespace std; class Foo { public: string data;
12
by: Bit byte | last post by:
I have an application written in C (actually PostgreSQL). The application appears to have been built using the Mingw set of tools (mingw compiler tools). I want to write an extension library...
2
by: toton | last post by:
Hi, This is continuation of topic pointer & reference doubt. http://groups.google.com/group/comp.lang.c++/browse_thread/thread/df84ce6b9af561f9/76304d7d77f6ccca?lnk=raot#76304d7d77f6ccca But I...
2
by: Subhransu Sahoo | last post by:
Hi All, Can anyone explain me why the return type of new MyClass is MyClass*. I think that it should be MyClass (*ptr) or MyClass**. Is it dependent on the compiler implementation of new ? ...
4
by: Marcin Kasprzak | last post by:
Hello Guys, Silly question - what is the most elegant way of compiling a code similar to this one? <code> typedef struct a { b_t *b; } a_t; typedef struct b {
10
by: r035198x | last post by:
There are a lot of common C mistakes. I have put together the ones I consider to be the silliest made by C programmers. These are usually the source of the popular C programming tears and remembering...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
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...

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.