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.
[without *] in my code,after compiling,it gives an error about typecasting,
Quote:
pcap.c:36:6: warning: assignment makes integer from pointer without a cast [enabled by default]
pcap.c:37:10: warning: comparison between pointer and integer [enabled by default]
is it because,that the ip and net are pointer so that i must use a pointer too,i mean this:
Code:
char *rip;
char *rnet;
if i want to type cast it i mean not to use a pointer just:
Code:
char rip;
char rnet;
what should i do?
how can i typecast it?
sorry for the long post,
thanks in advance
Click here to see the post LQ members have rated as the most helpful post in this thread.
Why do you want to do that? You should really learn about how pointers work.
Basically, a plain "char" holds one character (not a string). A "char*" contains a memory address. Why "char" before the asterisk, then? That means that when you "dereference" the pointer (get the value at the memory address it stores), it will be assumed that it's a "char" (You can use "void*" to just refer to a memory address without specifying any type).
So what does this have to do with strings? The idea is that a string is a sequence of "char"s in memory (with a NULL character at the end to indicate the end of the string), one after another, and the "char*" points to the first one. To get a character at the specified index, you add the character's index to the pointer and dereference it:
Code:
second_char = *(str + 1);
(The "*" operator dereferences a pointer)
And this is so common, that there's special syntax for it:
Code:
second_char = str[1];
(This is identical to the above example)
Hopefully now you understand a bit about how they work, and why "char" and "char*" are completely different things that are not interchangeable.
It seems to me that you need to acquire some deeper understanding of what these various declarations mean.
You need to understand what "char rip; means, and how it differs from "char *rip;" and what your source-code is saying to the compiler. It isn't enough to "change the code to make the syntax errors go away." (Either the symbol rip refers to a character, or it refers to a pointer to a character ... which one is it? You have to know.)
Typecasting, as a concept, is exactly the same thing. You have to understand what the use of this construct is saying to the compiler, and whether it actually matches what you want the computer to do and how the data is arranged in memory. "Nonsense in, garbage out."
Last edited by sundialsvcs; 07-03-2011 at 11:55 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.