![]() |
|
#1
|
|||
|
|||
If there is no sizeof operator in CI do not have much knowledge about compilers and all.
I have one simple question about C programming. If there is no sizeof operator in C then does those pointer arithmetic work or not ? I am very much confused about it. Please somebody help. |
|||
|
#2
|
|||
|
|||
Re: If there is no sizeof operator in CIn C we have the sizeof() operator that works same as a function.
See if this example of pointer math sheds some light. Feel free to ask more questions about what you don't understand about it. CPP / C++ / C Code:
Code:
|
|
#3
|
|||
|
|||
Re: If there is no sizeof operator in CI can understand the code and pointer arithmetic.......but my question is very specific to the compiler....
As you have mentioned.... Code:
but want to know if there is no sizeof operator in C then does it move 'int_p' sizeof(int) bytes ????? How it will know the size of the 'int' ???? |
|
#4
|
|||
|
|||
Re: If there is no sizeof operator in CQuote:
Magic!!! int a= 256; printf("%d \n", a; wow!!! |
|
#5
|
|||
|
|||
Re: If there is no sizeof operator in CQuote:
Why do you keep saying that there is no sizeof operator in C? Of course there is a sizeof operator in C. There has always been a sizeof operator in C. Howard posted a C program that uses the sizeof operator. (Did you try it?) What the heck are you talking about? But wait a minute! Are you saying that your C compiler won't compile a program that uses the sizeof operator? If so, then please tell us what compiler you are using, and please post the code. I gotta see it to believe it! (See Footnote.) Also, I would be interested in knowing what reference material you are learning from:
Regards, Dave Footnote: All standard C compilers have the sizeof operator. If there are any (non-standard) C compilers out that that don't have or recognize the sizeof operator, I would like to hear about them. Really. However... It is not true that all compilers have the same size of int variables. For some implementations, sizeof(int) may yield 4. For some it may yield 2. For some it may yield 8. (There may even be compilers that yield values other than these. Don't know; don't care.) In fact, that's one of the really good reasons that all C compilers need (and have) a sizeof operator. (That is: Sometimes a program must know the size of an int and act accordingly.) That may not seem important as you are reading introductory material, but you may find that it can come in handy time after time (after time) if/when you get to the Good Stuff of actually writing programs that are a little beyond Hello World!. I mean, I know that, in the beginning of the learning process, there is a certain amount of terminology and technical information that is introduced without much user motivation. (Like hinting at where you would ever use such a thing.) Pedagogically good material will show some meaningful usage as new concepts are revealed, but sometimes it is still not apparent to the beginner why such a thing is there. That's why I asked about the source of your learning material. __________________
Sometimes I just can't help myself... Last edited by davekw7x : 13-May-2012 at 13:00.
|
|
#6
|
|||
|
|||
Re: If there is no sizeof operator in CI don't think the discussion is going in correct direction. I know all C compilers have sizeof operator.
I have asked if there is no sizeof operator then what will happen. Think about a question..... If there is no sizeof operator how will you compute the size of int or any other datatype ? One simple answer is just take a pointer to that datatype and increment it once and take the difference with the previous value. But I want to know if there is no sizeof operator how the system will know how much to increment ??????? |
|
#7
|
|||
|
|||
Re: If there is no sizeof operator in CAlso
Quote:
First I must say that I don't really know exactly but my perception is this: The sizeof operator is something "WE" use in our C code to get the size of an object for use in our program. I BELIEVE The compiler doesn't "use" the sizeof() operator other than when it comes to it in our code it is replaced by a value. Where does this value come from? It is determined earlier in our code at declaration. When we say "int x;" an address is set up to store the int. The label "x" goes on a "list" along with that address and size of "int" on the target machine. When we write sizeof(x) that value is used in it's place. Quote:
int x; int * px = &x; Last edited by Howard_L : 14-May-2012 at 09:48.
|
|
#8
|
|||
|
|||
Re: If there is no sizeof operator in CQuote:
First of all, the sizeof operator is there to let a program know the size of "something." The C compiler knows the size of everything, regardless of the existence of any user operator. It has to. Obviously. I mean, it does calculations and storage access (and everything else that it needs to do) with whatever internal representation the compiler implementers decided to use. It does not require the existence of any particular user-accessible operator to do its work internally. On the other hand... Now, there are many situations for which a program has to be able to know the size of "something," and that is what the sizeof operator is for. Quote:
Well... Incrementing the pointer and attempting to determine the size by calculating the difference between new and old values won't work unless you cast the pointer to an integer data type. This is not portable, since there is no guarantee that casting a pointer to an integer won't lose information. I mean, if you take a pointer value and increment that value (using pointer arithmetic), and then you subtract the old value from the new value (using pointer arithmetic), you always get 1. On the other hand... If you cast the new and old pointer values to integer data types the program may be able to tell the difference (in bytes) by subtracting the integer values (using integer arithmetic). (However: See Footnote.) Here is an example that may work, but there are no guarantees: CPP / C++ / C Code:
Output (Compiled with 32-bit GNU gcc): Code:
Note that instead of casting, you could, conceivably, use sprintf-scanf to convert the pointer values to ints, but there is still no guarantee that an arbitrary pointer value can be converted to any particular integer data type without losing information. A final comment: Instead of casting pointers to integer data types, you might try casting them to pointers to char. It is guaranteed that sizeof(char) is equal to 1, so that doing pointer arithmetic with pointers to char might seem to be more portable. I'm not sure how C compilers are implemented on machines for which there is no byte-addressable memory (or even if it's worth trying such a thing) and how it relates to pointers to different data types. "Probably more portable" seems to be not particularly useful in the grand scheme of things. Bottom line: The sizeof operator is a portable way that is guaranteed always to return the correct size of "stuff" in C. It is easy to use and it is efficient, and, most importantly, it is portable. Regards, Dave Footnote: Beware: There is a lot (a lot) of legacy code out there that assumes a pointer has the same length as an int, but that is simply not always true. For 64-bit GNU compilers, pointers occupy 8 bytes but ints occupy 4 bytes. I have run across a lot (a lot) of legacy code that behaved "properly" on 32-bit systems but failed miserably when the code was recompiled with a 64-bit compiler. __________________
Sometimes I just can't help myself... Last edited by davekw7x : 14-May-2012 at 10:48.
|
|
#9
|
|||
|
|||
Re: If there is no sizeof operator in CQuote:
Like me, maybe you are not sure what the meaning of 'is' is. (See Footnote.) The answer is, "It depends." Regards, Dave "It depends on what the meaning of the words 'is' is." ---U.S. President William Jefferson (Bill) Clinton, during his 1998 grand jury testimony on the Monica Lewinsky affair. __________________
Sometimes I just can't help myself... |
Recent GIDBlog
Configuring iptables for Webmin Servers Index Module by gidnetwork
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The