Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 17-Nov-12 22:23pm
v2

There are a number of problems here: The first is that you are accessing memory you don't own:
C#
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[^]
 
Share this answer
 
Comments
_Vickey 18-Nov-12 10:07am    
Thanks,OriginalGriff
OriginalGriff 18-Nov-12 10:15am    
You're welcome!
While OriginalGriff's answer already says it all, let me try to clarify some more points here.

Your first pointer assignment is perfectly legal:
C++
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
C++
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.
C++
++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.
 
Share this answer
 
Comments
_Vickey 18-Nov-12 10:08am    
Thanks nv3,got it :)
nv3 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900