Click here to Skip to main content
11,581,210 members (61,200 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
#include <iostream>

using namespace std;
 
struct s1{
	int a;
	char ch;
}obj;
 
int main()
{
	int a1;
	s1* ptr=&obj;
	ptr->a=10;
	cout<<ptr->a<<endl;
	ptr->ch='x';
	cout<<ptr->ch<<endl;
	cout<<"address of 1st ptr"<<(long)ptr;//this will give address of first reference to object
	++ptr;//incrementing the pointer
	cout<<endl;
	ptr->a=20;
	cout<<"address of 2nd ptr"<<(long)ptr<<endl//this should give an incremented address as per the bytes hold by the structure i.e. 3 bytes but it is giving a jump of 8 bytes???? ;
	cout<<ptr->a;
 
	cout<<"---------------***********************************----------------"<<endl;
 
	long* lptr;
	long d=10;
	lptr=&d;
 
	cout<<sizeof (long)<<endl;
	cout<<"address of 1st long pointer is"<<(long)lptr<<endl;
	lptr++;
	cout<<"address of 2nd long pointer is"<<(long)lptr<<endl;//here it is fine working on primitive data types  just giving the jump of 4 bytes as it should give
	system("pause");
}

[edit]Code block added - OriginalGriff[/edit]
Posted 17-Nov-12 22:15pm
_Vickey159
Edited 17-Nov-12 22:23pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There are a number of problems here: The first is that you are accessing memory you don't own:
s1* ptr=&obj;
ptr->a=10;
...
++ptr;//incrementing the pointer
...
ptr->a=20;
Since obj is a single instance of the struct, and movement of the pointer points to an unknown location in memory - this can have drastic effects and will cause you enormous problems in the future.

The second is that you have a conception that your struct is only 3 bytes: it isn't. The compiler is at liberty to chose a size which is convenient to it or the target processor, rather than a simple addition of the bytes in the structure. This is called Structure Alignment[^]
  Permalink  
Comments
_Vickey at 18-Nov-12 10:07am
   
Thanks,OriginalGriff
OriginalGriff at 18-Nov-12 10:15am
   
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

While OriginalGriff's answer already says it all, let me try to clarify some more points here.

Your first pointer assignment is perfectly legal:
	s1* ptr=&obj;
Pointer ptr contains now the address of obj, hence it is pointing to obj.

Remark: Your question is somewhat unclear, probably because of language problems.This is no "reference" in the C++ sense. Perhaps you meant that ptr is now referencing object obj. Yes, one could say that.

With your static definition of obj
    struct s1{
	int a;
	char ch;
    } obj;
you have created just a single instance of obj and ptr is now pointing at it. When you now increment your pointer ptr, it will point somewhere undefined. The memory at this new location may contain just anything and by assigning data to this location you will be overwriting whatever is stored there.
	++ptr;//incrementing the pointer
	ptr->a=20;
This will write an integer 20 over whatever ptr is now pointing at. And most likely this will create some irratic behaviour of your program.

Perhaps you were assuming that incrementing the pointer would automatically lead to the allocation of an additional object of type s1. This is not the case. Memory space is only allocated when you define something or when you call new (or one of the malloc variants in C). By incrementing the pointer you just manipulate the pointer, nothing else.

Hope that clarifies it.
  Permalink  
Comments
_Vickey at 18-Nov-12 10:08am
   
Thanks nv3,got it :)
nv3 at 18-Nov-12 12:15pm
   
My pleasure!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 260
1 Frankie-C 145
2 Sergey Alexandrovich Kryukov 135
3 RyanDev 84
4 F-ES Sitecore 80
0 OriginalGriff 1,183
1 Sergey Alexandrovich Kryukov 825
2 Abhinav S 597
3 F-ES Sitecore 500
4 Suvendu Shekhar Giri 433


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100