Re: Error in Passing char pointer
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Tue, 25 Apr 2006 09:47:53 +0100
jeniffer wrote:
[$] gcc -c test.c
<OT>
To get make gcc behavio as a complient compiler and get a load more useful warnings use
gcc -c -ansi -pedantic -O -Wall test.c
or even
gcc -c -ansi -pedantic -O -Wall -W test.c
</OT>
[$] gcc -o test test.c
[$] ./test
i=6
0 14 22 42 48 e6
014224248ffffffe6
ea = [$]
I am not able to obtain the value of ea in main ....plz tell me how to
correct the problem
It would have helped if you had made some attempt at returning it. Did you expect it to appear in main by magic? See http://c-faq.com/ptrs/passptrinit.html
http://c-faq.com/misc/multretval.html
for some hints then try to do it. Also remember that automatic variables disappear when a function returns.
#include<stdio.h>
int main()
If you are not using the parameters it is better to state this
int main(void)
{
int i;
i=Set_Entry();
Always ensure that the compiler sees a prototype for each function before calling it. Either add the prototypes to the start of the file or rearrange the definitions of functions so they are defined before use.
return 0;
}
int Set_Entry()
{
char eaddr[]="0:14:22:42:48:e6";
unsigned char*ea;
char sa_data[14];
ea=(char*)sa_data;
Casts are evil. Never add a cast just to shut up the compiler, which I'm guessing is what you did, always understand why the compiler is complaining before changing anything. Why is ea unsigned char when everything else is signed char? Why do you even have this pointer variable?
if(ether_aton(eaddr,ea))
{
printf("\nreturning 1");
return (1);
}
printf("ea = %s",ea);
return 0;
}
ether_aton(a, n)
char *a;
char *n;
Don't use old style function definitions or implicit int. Always use the prototype for that you've used else where. Also your pointer types disagree with the pointers you are passing, unsigned char* and char* are not the same. Work out what you want to use and use it consistently.
{
int i, o[6];
i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
&o[3], &o[4], &o[5]);
printf("\n i=%d\n",i);
printf("%x %x %x %x %x %x",o[0],o[1],o[2],o[3],o[4],o[5]);
if (i != 6) {
perror("arp: invalid Ethernet address");
return (1);
}
for (i=0; i<6; i++)
n[i]=o[i];
Shouldn't you check that the numbers you have read are in range?
printf("\n");--
for(i=0;i<6;i++)
printf("%x",n[i]);
printf("\n");
return (0);
}
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
.
- References:
- Error in Passing char pointer
- From: jeniffer
- Error in Passing char pointer
- Prev by Date: Re: unprectable behaviour
- Next by Date: Binary files /DELETE/
- Previous by thread: Re: Error in Passing char pointer
- Next by thread: Binary files /DELETE/
- Index(es):
Relevant Pages
|