Re: Error in Passing char pointer



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
.



Relevant Pages

  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: Insufficient guarantees for null pointers?
    ... will the compiler know what the bounds are after converting that char * ... to an int *, if it could point to either of two arrays which happen to ... compares equal to the original pointer. ...
    (comp.std.c)
  • Re: problem with memcpy and pointers/arrays confusion - again
    ... int line, unsigned long *total_mem) ... That's a long pointer address... ... If sizeof > sizeof which is ... if you allocate for char with sizeof < sizeof, ...
    (comp.lang.c)
  • Re: Replacing fgets
    ... Even if u_int8_t is a typedef for unsigned char, ... Didn't your compiler complain here. ... offset is changed ... offset needs to be an int. ...
    (comp.lang.c)
  • Re: How to use a C++ class in .NET
    ... > absolutely compiler dependant. ... > public ref class MyClass ... > int funtion1(unsigned char* inBuffer, unsigned inType, unsigned char* ...
    (microsoft.public.dotnet.framework)