ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
INT, CHR, and FLT are all ints. enum just automatically numbers them for you.
I could just as easily have done:
#define INT 0
#define CHR 1
#define FLT 2
or:
int INT=0;
int CHR=1;
int FLT=2;
(At least I hope enum starts the numbering at 0; it might start at 1. Either way it's irrelevant as long as each has its own unique value )
Now that I think about it, that header code might not work on a different endian machine. It would work with the #define method or if I defined each one as a char though
So anyway, INT/CHR/FLT are just labels. They all have the same type and the value of each of them can fit into the 1-byte header (values = 0, 1, 2).
So we assume that INT is one byte long ? Ok , but how does pointer know how many bytes an element is long so it can move address correctly ( see my previous question for more details ) ?
Inside the fc() function in the type-dependent code, if the type is INT then it knows the next sizeof(int) bytes will be the integer.
So you could do:
Code:
case INT:
i = (int)(ptr+1);
Each type identifier (the first byte of ptr) will always only take 1 byte. The entire length of the ptr will be 1+<data length> bytes long to accommodate the 1 byte header.
So you can assume that if the type is INT then ptr will be 1+sizeof(int) bytes long, if FLT then ptr will be 1+sizeof(float) bytes long, and if CHR then ptr will be 1+strlen(str)+1(for \0) bytes long You don't need to know the length of the string ahead of time since we null-terminated it.) You could simply do:
Code:
case CHR:
strcpy(buf, (char *)(ptr+1));
Check out the memory arrangements in my first post.
Each type identifier (the first byte of ptr) will always only take 1 byte. The entire length of the ptr will be 1+<data length> bytes long to accommodate the 1 byte header.
So you can assume that if the type is INT then ptr will be 1+sizeof(int) bytes long, if FLT then ptr will be 1+sizeof(float) bytes long, and if CHR then ptr will be 1+strlen(str)+1(for \0) bytes long You don't need to know the length of the string ahead of time since we null-terminated it.)
That I understand
My question is how does pointer know where to point next . When you give (ptr+1) instruction , pointer must point to address that is away from the previous address by a number of bytes .How much bytes away it is ,depends on the element that pointer points to .If it is integer then pointer will move 4 bytes away and if it is char only 1 byte away . But the point is that pointer must know to which element it points to .
Code:
case CHR:
strcpy(buf, (char *)(ptr+1));
My question is how does pointer in the above example know that it must move only one byte away , since afterall it is generic pointer ?
C always increments pointers based on its type. void pointers always increment by 1. So ptr+1 means to add 1 byte. Whereas if you had an 'int *ptr;' and did ptr+1 it would move sizeof(int) bytes (usually 4).
Let's assume ptr is at address 0000 for the following:
void *ptr;
printf("%d + 1 = %d", ptr, ptr+1);
Should show: 0 + 1 = 1
But it was an int pointer it would be different:
Yes , that was my question . I didn't know void * pointers increment by 1 byte . I should ask more clearly , but it was pass midnight and my english sucks
Just thought I would point out that most system functions that return void pointers do so because they deal with data in bytes, and thus never have to worry about it's `type'. Also, the sizeof any pointer is going to be the same, it is a memory address. The idea with using void pointers is, as stated previously, to transparently deal with any data type - you should then be dealing with the memory in bytes, effectively. Malloc, for example, one of the most famous (void *) returning functions takes it's unsigned integer arguments as the number of bytes you want copied. You are expected to do the book keeping to then cast it back (implicitly or explicitly) when you are using it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.