Well, pointer arithmetic honors the data type!!
iterator = ds + 5;
will get the job done.
To elaborate, the above expression will produce a pointer by moving it 5 times, multiplied by the size of type for ds, in bytes. That's the same as &(ds[5]).
For sake of completeness, explaining why iterator = ds + (sizeof(dummy_struct)*5); is wrong, is, here, you're essentially trying to move the pointer to an element with index as (sizeof(dummy_struct)*5 which is, well out of bounds. Note please, this invokes undefined behavior!! note below
Quoting C11, chapter §6.5.6/P8
When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression
Ppoints to thei-th element of an array object, the expressions(P)+N(equivalently,N+(P)) and(P)-N(whereNhas the valuen) point to, respectively, thei+n-th andi−n-th elements of the array object, provided they exist. [....]
and then, regarding the undefined behavior mentioned above,
[....] If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary
*operator that is evaluated.
That being said, the printf() statement is also erroneous. You have two conversion specifiers, but supplied three arguments. It's not harmful, but useless/meaningless.
Related, from chapter §7.21.6.1/P2,
[...] If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored. [...]
Based on this case, you can directly use
printf("5th position %d:%s:\n",ds[5].id,ds[5].buffer);
Answer from Sourav Ghosh on Stack OverflowWell, pointer arithmetic honors the data type!!
iterator = ds + 5;
will get the job done.
To elaborate, the above expression will produce a pointer by moving it 5 times, multiplied by the size of type for ds, in bytes. That's the same as &(ds[5]).
For sake of completeness, explaining why iterator = ds + (sizeof(dummy_struct)*5); is wrong, is, here, you're essentially trying to move the pointer to an element with index as (sizeof(dummy_struct)*5 which is, well out of bounds. Note please, this invokes undefined behavior!! note below
Quoting C11, chapter §6.5.6/P8
When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression
Ppoints to thei-th element of an array object, the expressions(P)+N(equivalently,N+(P)) and(P)-N(whereNhas the valuen) point to, respectively, thei+n-th andi−n-th elements of the array object, provided they exist. [....]
and then, regarding the undefined behavior mentioned above,
[....] If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary
*operator that is evaluated.
That being said, the printf() statement is also erroneous. You have two conversion specifiers, but supplied three arguments. It's not harmful, but useless/meaningless.
Related, from chapter §7.21.6.1/P2,
[...] If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored. [...]
Based on this case, you can directly use
printf("5th position %d:%s:\n",ds[5].id,ds[5].buffer);
The behaviour of
iterator = ds + (sizeof(dummy_struct) * 5);
is undefined if you set a pointer beyond one past the final element of the array. You are actually setting iterator to further elements in the array than you think, by a factor of sizeof(dummy_struct)! Don't do that.
The language allows you to use the notation iterator = ds + 5;. This idiomatic pointer arithmetic adds 5 lots of sizeof(dummy_struct) to the address iterator. In other words the compiler does the sizeof adjustment for you. That's one reason why pointer arithmetic is so powerful.
Finally note that *(ds + 5) is equivalent to ds[5]. I find the latter affords more clarity when working with arrays.
Move pointer in arrays with C - Stack Overflow
how to move file pointer to a particular line in c
using a function to move a pointer(I thi - C++ Forum
c - How to increment a pointer address and pointer's value? - Stack Overflow
Im wondering why if I moved the pointer of str_out to the end, thus not being able to print the string with ` printf("%s", str_out)` in remove_exclamation(), how come I can do ` printf("%s", str_out)` in the main ().
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
void remove_exclamation_marks(const char *str_in, char *str_out) {
char *point = str_out;
while (*str_in) {
if(*str_in != '!')
*str_out++ = *str_in;
str_in++;
}
*str_out = '\0';
printf("%s", str_out); // doesnt print anything bc pointer pointing at the \0
printf("%s", point); // begin of str_out
}
int main() {
char str_in [] = "Hello W!orld!!";
char str_out [strlen(str_in)];
remove_exclamation_marks(str_in, str_out);
printf("%s", str_out);
}First, the ++ operator takes precedence over the * operator, and the () operators take precedence over everything else.
Second, the ++number operator is the same as the number++ operator if you're not assigning them to anything. The difference is number++ returns number and then increments number, and ++number increments first and then returns it.
Third, by increasing the value of a pointer, you're incrementing it by the sizeof its contents, that is you're incrementing it as if you were iterating in an array.
So, to sum it all up:
ptr++; // Pointer moves to the next int position (as if it was an array)
++ptr; // Pointer moves to the next int position (as if it was an array)
++*ptr; // The value pointed at by ptr is incremented
++(*ptr); // The value pointed at by ptr is incremented
++*(ptr); // The value pointed at by ptr is incremented
*ptr++; // Pointer moves to the next int position (as if it was an array). But returns the old content
(*ptr)++; // The value pointed at by ptr is incremented
*(ptr)++; // Pointer moves to the next int position (as if it was an array). But returns the old content
*++ptr; // Pointer moves to the next int position, and then gets accessed, with your code, segfault
*(++ptr); // Pointer moves to the next int position, and then gets accessed, with your code, segfault
As there are a lot of cases in here, I might have made some mistake, please correct me if I'm wrong.
EDIT:
So I was wrong, the precedence is a little more complicated than what I wrote, view it here: http://en.cppreference.com/w/cpp/language/operator_precedence
checked the program and the results are as,
p++; // use it then move to next int position
++p; // move to next int and then use it
++*p; // increments the value by 1 then use it
++(*p); // increments the value by 1 then use it
++*(p); // increments the value by 1 then use it
*p++; // use the value of p then moves to next position
(*p)++; // use the value of p then increment the value
*(p)++; // use the value of p then moves to next position
*++p; // moves to the next int location then use that value
*(++p); // moves to next location then use that value
Haven’t touched cpp. Does adding 1 move the address by 4 bytes?