C --\++ Operator
C --\++ Operator
Hi all,
I just have a question about the ++ operator, if I have an array of longs say long data[25], if I go data++ does this move me to the next element (ie 4 bytes) or to the next byte?
thanks.
Also, I hope that these c questions are not to far off topic here, just let me know if it is and will not post them here. However they are directly related to my OS programming, I am not learning C just for the sake of it, I am doing it only for OS programming.
I just have a question about the ++ operator, if I have an array of longs say long data[25], if I go data++ does this move me to the next element (ie 4 bytes) or to the next byte?
thanks.
Also, I hope that these c questions are not to far off topic here, just let me know if it is and will not post them here. However they are directly related to my OS programming, I am not learning C just for the sake of it, I am doing it only for OS programming.
Re:C --\++ Operator
if you do a "array[ i + 1 ]" and i = 4, you have the fourth -1 array ( c uses 0 as the first one )
and you can do array++ to go to the next thing in the array, hope that helps,
and you can do array++ to go to the next thing in the array, hope that helps,
Re:C --\++ Operator
if you increment or decrement a pointer (that's kinda what array's are, pointers to a block of data), it increases it by the size of the data type.
so if I have an struct like this:
the ptr will be incrememted by however many bytes the struct is (including alignment i *think).
subtracting pointers will also give you the number of datatypes.. not the number of bytes.
It gets tricky with void* pointers and arithmetic though. I know GCC defaults to char* size but other compilers may vary.
- Nick
so if I have an struct like this:
Code: Select all
struct largething
{
int something;
int more[100];
}
struct largething *ptr;
ptr++;
subtracting pointers will also give you the number of datatypes.. not the number of bytes.
It gets tricky with void* pointers and arithmetic though. I know GCC defaults to char* size but other compilers may vary.
- Nick
Re:C --\++ Operator
Thanks guys, I think some people do not like me posting C questions here so I will not do it anymore.
Thanks for your help.
Thanks for your help.
Re:C --\++ Operator
I can't promise this is right, but I'll try.PlayOS wrote: Hi all,
I just have a question about the ++ operator, if I have an array of longs say long data[25], if I go data++ does this move me to the next element (ie 4 bytes) or to the next byte?
As the question is stated, this won't work at all. Bases of declared arrays are not quite the same as pointers; among other things, their values are constant. From an assembly language POV, an array base is a label, whereas a pointer is a memory location in which an effective address has been stored. The code
Code: Select all
any_type data[23];
data++;
will give a type mismatch on the second line with most modern compilers.
OTOH, if you declared a pointer [tt]any_type *data_p;[/tt] and assigned the base of [tt]data[/tt] to it, then yes, according to the rules of pointer arithmetic, it increments by [tt]sizeof(any_type)[/tt]. Just remember that if the array is auto to the function, it only lasts as long as the function stack frame.
On a related note, while it is legal to do this:
Code: Select all
any_type data_curr[]; /* pointer to any_type */
data_curr = (any_type *)malloc(sizeof(any_type) * 23);
data_curr++;
Code: Select all
any_type *data_curr, data_top;
data_top = data _curr = (any_type *)malloc(sizeof(any_type) * 23);
data_curr++;
free(data_top);
Well, they technically are OT, but they are close enough that they could be considered relevant. Posting C specific group may get faster and more exact results, however.Also, I hope that these c questions are not to far off topic here, just let me know if it is and will not post them here. However they are directly related to my OS programming, I am not learning C just for the sake of it, I am doing it only for OS programming.
Re:C --\++ Operator
Thanks for your response, I agree that they are technically off topic but, I think that they are no more off topic than questions about gcc or ld or the use of any program, but these kind of questions are coming in this forum all the time and no one says nothing.
People feel that they can ask them here because they are developing an OS, they run into trouble, so they ask. I personally dont think that questions about gcc, ld or C are off topic if you are using or needing the knowledge to build your OS.
But if people dont want me to do it, then I wont. It's just that simple.
Thanks again.
People feel that they can ask them here because they are developing an OS, they run into trouble, so they ask. I personally dont think that questions about gcc, ld or C are off topic if you are using or needing the knowledge to build your OS.
But if people dont want me to do it, then I wont. It's just that simple.
Thanks again.
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:C --\++ Operator
hmmm ... perhaps we should split this forum in 2 or 3 parts: one for tools/language/etc. questions, one for hardware/pmode/etc. questions and a last one for generic design issues ...PlayOS wrote: Thanks for your response, I agree that they are technically off topic but, I think that they are no more off topic than questions about gcc or ld or the use of any program, but these kind of questions are coming in this forum all the time and no one says nothing.
People feel that they can ask them here because they are developing an OS, they run into trouble, so they ask. I personally dont think that questions about gcc, ld or C are off topic if you are using or needing the knowledge to build your OS.
Re:C --\++ Operator
I think it is not a good idea to divide forum because I am not looking for a C forum, I am looking for an OS dev. forum. So I think question that is argued here is completely out of interest of this forum. PlayOS you can get answer to your question by yourself very easily by just writing a simple (but intelligent) C program. I mean you could test your data[25] in the time you write the question here. Thanx...
Re:C --\++ Operator
I think a separate programming language forum is a good idea, since the posts here tend to be either OS theory or actual code. Maybe three: OS theory, x86 stuff ("how do I use V86?") and programming languages.
Still, if df is away (is he?), we'll get no new fora.
Still, if df is away (is he?), we'll get no new fora.
Re:C --\++ Operator
Ozguxxx, you are right, at least you come up with a reason. I have noticed the new programming forum that is here now, I will post all such questions there.
Tim, I think that is a good idea to have seperate forums for different topics, this way you could have beginner stuff in one, more advanced topics in another and then really advanced stuff in another.
However, I am glad that there is a new forum just for programming topics that are not specific to OS Dev.
Tim, I think that is a good idea to have seperate forums for different topics, this way you could have beginner stuff in one, more advanced topics in another and then really advanced stuff in another.
However, I am glad that there is a new forum just for programming topics that are not specific to OS Dev.