-2

How can I increment the address of the pointer which is defined in structure? I have defined a pointer in the main function that points to the structure. If I use ptr->element, where element is a pointer inside the structure, is it referring to the value pointed by the element? If yes, how can make the element point to the next location?

Antti29
  • 2,953
  • 12
  • 34
  • 36
  • It would help if you added your code to the question. See [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) in the [help center](https://stackoverflow.com/help). – user3386109 Oct 14 '17 at 15:47
  • I am using ptr->element, but it seems it is accessing the value pointed by element. What I want is to increment the element so as to point to next location. – Vikrant Waje Oct 14 '17 at 15:47
  • 1
    show us your code. – krpra Oct 14 '17 at 15:49
  • hey, I have added the code by editing my post. Just click on enter image description – Vikrant Waje Oct 14 '17 at 15:54
  • @VikrantWaje, please don't post screenshots of code. It is very easy to post code itself, which makes it much easier for people to answer your question. see https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks – Nik Oct 14 '17 at 16:19
  • So what are you trying to increment? Are trying to increment the `char*` member, as in go character to character until you reach `'/0'`, are you trying to increment to the next struct member? What is your goal? – Nik Oct 14 '17 at 16:27
  • I am trying to increment the address of char *member ,not the value . I do not want to increment to next struct member. How should I do it? – Vikrant Waje Oct 14 '17 at 16:31

2 Answers2

0

It's implementation-specific, but in practice the rule is:

Struct members are stored in the order they are declared. If necessary, padding is added before each struct member, to ensure correct alignment.

Each primitive type Ntype requires an alignment of sizeof(Ntype) bytes.

So, given the following struct:

struct Ntype
{
   char ch1;
   short sh;
   char ch2;
   int *a;
};
  • ch1 is at offset 0
  • a padding byte is inserted to align
  • sh at offset 2
  • ch2 is at offset 4, immediately after s
  • 3 padding bytes are inserted to align...
  • a at offset 8

So sizeof(Ntype) is 16.

krpra
  • 466
  • 1
  • 4
  • 19
  • in above program, how can i refer to address of pointer* a in struct Ntype (increment or decrement the address) using pointer from main function. I know we can access the value of a using following construct: mainptr->a. I am confused how can i increment the address of a – Vikrant Waje Oct 14 '17 at 16:14
0

To increment the address of the char* member, You would do the following:

//assuming that CB_t is already allocated.
char *temp = CB_t->buffptr;
//now you can increment temp as so
char myChar = *temp;
temp++;
char nextChar = *temp;
temp++;//and so on. You can also put it in a loop, where you end condition would be encountering a '\0' character.

Don't do this: CB_t->buffptr++ as it will ruin your structure! If you do that, then buffptr will be trimming leading characters from member.

You can also do this:

char firstChar = *CB_t->buffptr;
char secondChar = *(CB_t->buffptr + 1);
char thirdChar = *(CB_t->buffptr + 2);//and so on

I would recommend you read this discussion about pointer arithmetic. It can be a tough concept to wrap your head around, but once you do you'll be armed with a very powerful weapon!

Nik
  • 1,780
  • 1
  • 14
  • 23