Error in Passing char pointer

J

jeniffer

[$] gcc -c test.c
[$] 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

#include<stdio.h>
int main()
{
int i;
i=Set_Entry();
return 0;

}
int Set_Entry()
{
char eaddr[]="0:14:22:42:48:e6";
unsigned char*ea;
char sa_data[14];
ea=(char*)sa_data;
if(ether_aton(eaddr,ea))
{
printf("\nreturning 1");
return (1);
}
printf("ea = %s",ea);
return 0;

}

ether_aton(a, n)
char *a;
char *n;
{
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=o;


printf("\n");
for(i=0;i<6;i++)
printf("%x",n);
printf("\n");
return (0);
}
 
V

Vladimir Oka

jeniffer said:
[$] gcc -c test.c

gcc -c -pedantic -ansi -Wall

Would have told you many useful things.
[$] 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

Yes you are. See below.
....plz tell me how to correct the problem

"plz" is not valid English.

However, well done for paring the problem down. It also almost
compiles. ;-)
#include<stdio.h>

You should really declare your functions here:

int Set_Entry(void);
int ether_aton(char *, char *);
int main()

Spell it out:

int main(void)
{
int i;
i=Set_Entry();
return 0;

}
int Set_Entry()
{
char eaddr[]="0:14:22:42:48:e6";

Try:

char eaddr[]="1:14:22:42:48:e6";

instead. Your "%s" ran into a 0 and figured out that was the end of
string. Goes to show that test cases have to be well thought out.
unsigned char*ea;

Your spacing here is atrocious.

unsigned char *ea;
char sa_data[14];
ea=(char*)sa_data;

Why do you need this?
if(ether_aton(eaddr,ea))
{
printf("\nreturning 1");
return (1);
}
printf("ea = %s",ea);

Did you think about leaving room for a terminating '\0' (and supplying
one as well) if you wanted to do this? That's how C strings are
delimited.
return 0;

You need to terminate output with '\n' or `fflush()' if you want to be
sure something does get output.
}

ether_aton(a, n)
char *a;
char *n;

int ether_aton(char *a, char *n)
{
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]);

Here, you've used unsigned conversion for a signed variable. Your `o`
should really be unsigned.
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);

`return` is not a function -- don't parenthesise it.
}
for (i=0; i<6; i++)
n=o;


printf("\n");
for(i=0;i<6;i++)
printf("%x",n);
printf("\n");
return (0);
}


There's probably more to be said about spacing and general coding
style, but I'll leave that (and everything else i missed) to somebody
else.
 
F

Flash Gordon

jeniffer said:
[$] 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
[$] 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=o;


Shouldn't you check that the numbers you have read are in range?
printf("\n");
for(i=0;i<6;i++)
printf("%x",n);
printf("\n");
return (0);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,511
Latest member
svareza

Latest Threads

Top