473,791 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error in Passing char pointer

[$] gcc -c test.c
[$] gcc -o test test.c
[$] ./test

i=6
0 14 22 42 48 e6
014224248ffffff e6
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_da ta;
if(ether_aton(e addr,ea))
{
printf("\nretur ning 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[i]=o[i];
printf("\n");
for(i=0;i<6;i++ )
printf("%x",n[i]);
printf("\n");
return (0);
}

Apr 25 '06 #1
2 2441

jeniffer wrote:
[$] 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
014224248ffffff e6
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_da ta;
Why do you need this?
if(ether_aton(e addr,ea))
{
printf("\nretur ning 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[i]=o[i];
printf("\n");
for(i=0;i<6;i++ )
printf("%x",n[i]);
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.

Apr 25 '06 #2
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
014224248ffffff e6
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_da ta;
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(e addr,ea))
{
printf("\nretur ning 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
Apr 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10182
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
7
4929
by: mattsniderppl | last post by:
Hi, i'm relatively new to C++ from java and am having a difficult time with pointers. I'm sure there is something simple that I am doing wrong, but I can't seem to write this in a way that doesn't give me the error "Unable to write to memmory." This is a static function that receives a string that is the name of a file and an ifstream pointer. It then concatenates the path string using the string.h function strcat(char *, const char *) and...
12
2005
by: Neil Dunn | last post by:
Hi, I don't seem to be able to get asprintf to work when compiling under OS X. Using the following code: #include <string.h> #include <stdio.h>
12
5405
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
13
2486
by: a.zeevi | last post by:
free() multiple allocation error in C ==================================== Hi! I have written a program in C on PC with Windows 2000 in a Visual C environment. I have an error in freeing multiple allocation as follows: 1. I allocated an array of pointer. 2. I Read line by line from a text file. 3. I allocated memory for the read line.
33
2871
by: Martin Jørgensen | last post by:
Hi, In continuation of the thread I made "perhaps a stack problem? Long calculations - strange error?", I think I now got a "stable" error, meaning that the error always seem to come here now (tried: visual studio 2005 + linux/macintosh gcc)... That's a pretty good thing. I think the error still appears using both gcc and visual studio 2005. Everything is standard C (ANSI C ?? I don't know the difference) - but since so many functions...
3
3495
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/ #include <cstdio> #define MAX_WORD_LEN 80 #define MAX_SIZE 1000
21
6898
by: one2001boy | last post by:
PostMessage() function returns ERROR_NOT_ENOUGH_QUOTA after running in a loop for 700 times, but the disk space and memory are still big enough. any suggestion to resolve this problem? thanks.
2
4438
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p * len(id) # id is a list of strings Id_dat=StringVector() for i in range(len(Id)): ....Id_dat=id
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.