LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-09-2003, 01:23 AM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
pointer to structure


I have two questions about the program that is at the bottom of the post:
First, if I write a program
Code:
int* ip=&i;
then I can use
Code:
cout<<ip;
to output the address of i; similar to this, can I output the address of s1.c by the command that contains the words sp1? I ask this because I have written
Code:
structure3* sp1=&s1;
Second, is the address of s1.c really aB? Why is not some value as 0x... as the address of s1.i?
Code:
#include<iostream>
using namespace std;

typedef struct struct3 {
   char c;
   int i;
} structure3;
int main ()
{
   structure3 s1, s2;
   structure3* sp1=&s1;
   structure3* sp2=&s2;
   s1.c='a';
   s1.i=1;
   sp2->c='b';
   sp2->i=2;
   
   cout<<"sp1->c="<<sp1->c<<endl
       <<"sp1->i="<<sp1->i<<endl;
   cout<<"s2.c="<<s2.c<<endl
       <<"s2.i="<<s2.i<<endl;
   cout<<"&s1.c="<<&s1.c<<endl
       <<"&s1.i="<<&s1.i<<endl;
}

[root@localhost a_pointers]# ./simple_struct3_c
sp1->c=a
sp1->i=1
s2.c=b
s2.i=2
&s1.c=a
B
&s1.i=0xbfffda44
[root@localhost a_pointers]#
Thank you.

Last edited by Xiangbuilder; 10-09-2003 at 01:29 AM.
 
Old 10-09-2003, 02:00 AM   #2
dimm_coder
LQ Newbie
 
Registered: Oct 2003
Location: Minsk, Belarus
Distribution: Mandrake, FreeBSD
Posts: 28

Rep: Reputation: 15
Re: pointer to structure

Quote:
Originally posted by Xiangbuilder
cout<<"&s1.c="<<&s1.c<<endl
}
[root@localhost a_pointers]#[/code]Thank you. [/B]
c is a char, so &s1.c is interpreted like char* and cout think that it has to write a string (char*) and it so this.

int * a = new int;
cout << a << endl; // put the address of a (int*)

char * s = "i'm a string";
cout << s << endl; // put a string, not the address of s;

So if U want to get the address,
U need to write cout << (int*)(&s1.c) << endl;
 
Old 10-09-2003, 03:39 AM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.
I feel I can understand some parts of your post,
and I can't understand others because of my poor c++ knowledge.
Howeve, I have downloaded it for future learning.
Thank you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
structure os2 Programming 6 05-29-2005 07:39 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
pointer to pointer question in c lawkh Programming 2 01-29-2004 10:26 AM


All times are GMT -5. The time now is 09:51 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration