473,597 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

related to pointer

23 New Member
Hi,
I have a very basic problem related to strings using pointers in c++
//////////////////
i wrote code as,
int *i_ptr;
char *c_ptr;

int i=5;char c='A';
i_ptr=&i;
c_ptr=&c;

cout<<i_ptr;
cout<<c_ptr;

As an o/p i get an address of i_ptr but c_ptr doesn't store any addr WHY?

How sting are stored in memory(1 dim &2dim both) &how pointers access them?

As when we write a[1] we get addr if a is an array of pointers but in case of strings it returns string n not the address ,how?
Sep 27 '07 #1
10 1581
mschenkelberg
44 New Member
I think it is because a char * is always interpreted as a "C String" , so it will try to output values until it reaches a null character. I think if you do something like &c_ptr, it will show the address
Sep 28 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
I think if you do something like &c_ptr, it will show the address
Nope. That will get the address of c_ptr but not the address inside c_ptr.

You are correct though, in that the operator<< for char* assumes a C-style string.

What you have to do is create an int out of the pointer.
Expand|Select|Wrap|Line Numbers
  1.  
  2. unsigned int result(c_ptr);
  3.  
  4.  
then you can display result and see the address.

This syntrax shown is the constructor form for creating a variable. It is equivalent to doing:
Expand|Select|Wrap|Line Numbers
  1. cout << static_cast<unsigned int> (c_ptr);
  2.  
Sep 28 '07 #3
ashwini1680
23 New Member
I think it is because a char * is always interpreted as a "C String" , so it will try to output values until it reaches a null character. I think if you do something like &c_ptr, it will show the address
Hi,
Thank you.I changed my code.now it's working.Still i am confused with 2d array of char using pointers.
Sep 28 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
Still i am confused with 2d array of char using pointers.
Start a new thread. This is a new question.
Sep 29 '07 #5
ashwini1680
23 New Member
Hi,
I have a very basic problem related to strings using pointers in c++
//////////////////
i wrote code as,
int *i_ptr;
char *c_ptr;

int i=5;char c='A';
i_ptr=&i;
c_ptr=&c;

cout<<i_ptr;
cout<<c_ptr;

As an o/p i get an address of i_ptr but c_ptr doesn't store any addr WHY?

How sting are stored in memory(1 dim &2dim both) &how pointers access them?

As when we write a[1] we get addr if a is an array of pointers but in case of strings it returns string n not the address ,how?


As the replies I got for the same problem i changed my code as
cout<<&c_ptr
but it gives the addr of pointer & not the addr of te var to which it points.
Where the addr is stored?
as we initialises pointer with the addr.
Same if i have

char *a[]={"Pointer","Co mputer","Scienc e"};
then
printing
a[0],a[1],a[2] gives me strings themseleves.How ?

because to swap the strings 2 & 3,we use
char *t;
t=a[1];
a[1]=a[2];
a[2]=t;
here it takes addresses & if we print a[0],a[1] we get string .I don't understand this whole concept.I am very Congused.Help me.
Sep 29 '07 #6
shabinesh
61 New Member
Hi,
*a[],**a,a[][] all means the same in their concept.
*a and a[] means the same ,so you needn't use any pointer for printing the values in "a",
char *t;
t =a[1];
a[1]=a[2];
a[2]=t;
i suppose its a mistake ,it should
char *t;
* t=a[1];
a[1]=a[2];
a[2]=*t;
for better refrence, check out 'The C Programming language' by K&R.
Sep 30 '07 #7
ashwini1680
23 New Member
Hi,
*a[],**a,a[][] all means the same in their concept.
*a and a[] means the same ,so you needn't use any pointer for printing the values in "a",
char *t;
t =a[1];
a[1]=a[2];
a[2]=t;
i suppose its a mistake ,it should
char *t;
* t=a[1];
a[1]=a[2];
a[2]=*t;
for better refrence, check out 'The C Programming language' by K&R.



hi,
I checked my code.It's correct,I have taken it from a book.
the point of confusion for me is that,
pointer var always stores addresses.
so when we write,
int *ptr;
int i=10;ptr=&i;
then if we write,
cout<<ptr
we get address of int i stored in ptr.
but in case of char
like

char *ptr;
char c='a'
ptr=&c
or
char *ptr
char c="pointer"
ptr=&c
Now if we print value of ptr we dont get address.Why?

if c is a char variable we get some strange characters & if c is a string then we get the string itself. How???
Sep 30 '07 #8
Laharl
849 Recognized Expert Contributor
In C, arrays of characters (also referred to syntactically with char*), are the only available implementations of strings. These strings end with a special character, '\0', and when a char* is passed to <<, it expects one of these C-style strings, even in C++, so it prints until it sees \0. Since in this case, it doesn't, you get weird results as the "array" goes out of bounds.
Oct 1 '07 #9
shabinesh
61 New Member
char *ptr;
char c='a'------------------(1)bug here
ptr=&c
or
char *ptr
char c="pointer"---------------(2) bug here
ptr=&c

(1).The char "a" is not stored in the var c the integer value is stored since you use a single quotes('a').
(2).The char c cannot hold a string it can hold only a character.
Oct 1 '07 #10

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

Similar topics

5
2302
by: Rob Ristroph | last post by:
Hi, It's pretty unhelpful to post "I have a huge piece of code that crashes in strange places, what's the problem?" but that's basically my problem and I really am at my wit's end. The piece of code in question always crashes in an STL operation such as a vector.push_back, but the location of the crash changes as I change how various parts are handled in memory, i.e., make some things dynamically allocated instead of new'd. I know...
8
3367
by: Chul Min Kim | last post by:
Hi, I got a BUS ERROR from one of my company's program. Let me briefly tell our environment. Machine : Sun E3500 (Ultra Sparc II 400Mhz CPU 4EA) OS : Solaris7 Compiler : Sun Workshop 5.0 cc complier I get "BUS ERROR" only when I execute the binary which builds
6
1468
by: johnnieboy | last post by:
I am trying to compile the program hull http://cm.bell-labs.com/netlib/voronoi/hull.html This is quite a complicated program that involves several .c files I get several warnings during compilation and the error: hullmain.c:40: initializer element is not constant This is the offending line: FILE *INFILE, *OUTFILE, *DFILE = stderr, *TFILE;
6
4399
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the same invocation from a DistantlyRelated class. What actually happened was that the compiler produced: error C2247: 'A::function' not accessible because 'CloselyRelated' uses 'private' to inherit from 'A' I'm guessing that the above compiler...
5
1781
by: RKS | last post by:
I am new to Com programming using C++. Can pointers be passed through interface functions. I would like to pass multi dimensional arrays to functions defined in the com interface. Any help is greatly appreciated. Thanks P.S It this topic is completely unrelated to this news group, I am sorry.
8
1872
by: gk245 | last post by:
This is part of a bigger program, but i made it simple (basically the OT_hours function is supposed to determine if the number entered is over 40 hours or not and return the appropriate answer): #include <stdio.h> #include <stdlib.h> struct person { float overtime;
4
1258
by: somenath | last post by:
Hi all, Can any one let me know what will be the out put of the bellow mentioned program and why it is so . typedef struct { int day; int month; int year; } MYDATE;
2
3955
by: zl2k | last post by:
hi, all I need to use gsl_vector pointer with std::vector but not sure how to free the memory when I don't need it. Here is a piece of the code. =================== std::vector<gsl_vector * v; gsl_vector * a = gsl_vector_alloc(3); gsLvector_set(a, 0, 7); v.push_back(a);
5
1341
by: deba | last post by:
Hi i want to know is it possible to delete the node where my pointer currently pointing without knowing the head node address.Dis question ask to me in an interview. I really want to know the soloution of it.Thanx
1
1680
by: somenath | last post by:
Hi All, I have couple of questions regarding the behavior of the bellow code. #include<stdio.h> int main(void) { int ar={ {1,2}, {3,4}, {4,5}
0
7969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7886
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8035
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5431
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3886
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3927
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2404
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 we have to send another system
0
1238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.