LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-03-2011, 09:44 AM   #1
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 136

Rep: Reputation: 20
typecasting


hello everyone,
i'm totally a newbie,i'm trying to learn network programming with c,i right a very simple program which use libpcap,
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>  
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
char *dev;
char err[PCAP_ERRBUF_SIZE];
int r;
bpf_u_int32 ip;
bpf_u_int32 net;
struct in_addr addr;
char *rip;
char *rnet;

dev=pcap_lookupdev(err);
if (dev==NULL){
printf("%s",err);
exit(1);
}
else
{
printf("Device Name Is: %s\n",dev);
}

r=pcap_lookupnet(dev,&ip,&net,err);
if(r==-1){
printf("%s,err");
exit(1);
}

addr.s_addr=ip;
rip=inet_ntoa(addr);
printf("%s\n",rip);
when i use
Code:
char rip; 
char rnet;
return 0;
}
[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.
Old 07-03-2011, 11:01 AM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
First of all, indent your code. It's almost impossible to read un-indented code, and it's very easy to to indent it as you write.

Quote:
Originally Posted by z99 View Post
when i use
Code:
char rip; 
char rnet;
return 0;
}
[without *]
I don't see that in your code. Could you give some more details?

And what do you want to do, cast a "char*" to a "char"? That's not really valid, since they store very different kinds of data.
 
Old 07-03-2011, 11:34 AM   #3
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 136

Original Poster
Rep: Reputation: 20
thanks for helping,
i mean instead of using
Quote:
char *rip;
char *rnet;
use:
Quote:
char rip;
char rnet;
is it possible,no casting will be needed?
 
Old 07-03-2011, 11:51 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by z99 View Post
thanks for helping,
i mean instead of using

use:

is it possible,no casting will be needed?
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.
 
2 members found this post helpful.
Old 07-03-2011, 11:54 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,691
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Typecasting void pointer saurabhchokshi Programming 5 04-14-2010 01:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration