Link to home
Start Free TrialLog in
Avatar of farekat
farekat

asked on

UNIX C programming variable manipulation

UNIX korn shell
I have a Korn shell program I am writing where I would like to pass a variable
number to a C program.  This will get passed as a char variable but I would like
to convert it to a long

This is my call to the C program from Korn

GETCUSTIDAPP 12345

This will get passed into the main via:

int GETCUSTIDAPP(int argc, char **argv)

Where I set the variable passed in to a variable called

char custId[10]

Now, I need to pass in this custId to another function Get_Address and convert it to a long I'm having trouble with it understanding how to do it:

Here is what I have:
int Get_Address(char* customer) --> is this the right way to pass it in?
Also how do I convert this value to a long within the function?  I need to use this value in a sql call as a number later in the code.

I hope this made some sense.
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Use scanf to convert from a char[] to a long.
http://www.cplusplus.com/reference/clibrary/cstdio/scanf.html
Something like below...

long nCustId = 0;
scanf("%ld", custId);

Open in new window

if (argc < 2)
{
 fprintf(stderr, "Need parameter");
 exit(1);
}

strncpy(custId, argv[1], 10);
custId[9] = 0; //good habit

long num = strtol(custId, NULL, 10);
Sorry, my head is somewhere else... ignore above! I'll try again!

Use sscanf to convert from a char[] to a long.
http://www.cplusplus.com/reference/clibrary/cstdio/sscanf.html
Something like below...

long nCustId = 0;
sscanf(custId, "%ld", &nCustId);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jkr
> strcpy(custId,argv[1]);
is vulnerable to buffer overflow easily.