• C

Copying argc value to a integer variable

I am working on Sun Server 5.6 Generic sparc SUNW,ultra computer and my program assigns the value of argv[1] to a integer variable. This is not accepted by compiler and it core dumps exactly at this line
int static a;
a = atoi(argv[1]);
Can u explain why
jmaheshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

baegCommented:
what is argv defined, it nust be like this:

int main(char *argv[], int argc) {
...
int static a=0;
   if (argc>=1 && argv[1]) a=atoi(argv[1]);
...
}

if the problem is persist, make a copy from argv[1]

char s[1024];
  strcpy(s,argv[1]);

because you may have another heap.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cpopinCommented:
>> int static a;

Should this be:

static int a;

Also, is there an argument on the command line?  In other words, check to see if argv[1] is NULL.
0
KangaRooCommented:
List your arguments:
  for(int i =0; i< argc; ++i) printf("%s\n", argv[i]);
and check the format.

And its
  int main(int argc, char* argv[])
0
The Lifecycle Approach to Managing Security Policy

Managing application connectivity and security policies can be achieved more effectively when following a framework that automates repeatable processes and ensures that the right activities are performed in the right order.

jmaheshAuthor Commented:
I tried the way baeg wanted me to

But still it core dumps. I am using dbx tool to verify the line where it core dumps . It shows me

strcpy(s,argv[1]);

which is clueless. I printed all my arguments and it is printing correctly
0
KangaRooCommented:
I would like to point out that baeg has the two arguments reversed (according to my manual at least).
0
ozoCommented:
baeg had the args backward.  as KangaRoo said, it's
int main(int argc, char* argv[])
0
marcjbCommented:
>> if (argc>=1 && argv[1]) a=atoi(argv[1]);

argc >= 1 is ALWAYS true, since the name of the program counts as an argument.

if ( argc > 1 )
  a = atoi(argv[1]);

Also, there is no reason to define the variable a in main as static.

#include<stdlib.h>

int main(int argc, char* argv[])
 {
 int a;

 if ( argc > 1 )
  a = atoi(argv[1]);

 reutrn 0;
 }
0
jmaheshAuthor Commented:
The code is exactly same as marcjb but the sun server behaves differently.

Do u know of any sun server debugger to debug which line exactly it core dumps. I used dbx and it gives me the
a = atoi(argv[1]); as the core dump line . to me it is perfectly ok and I do not know why it core dumps
0
marcjbCommented:
How big is argv[1]?
0
marcjbCommented:
I don't mean how big the number is, but how many characters are passed.  What is the command line?
0
jmaheshAuthor Commented:
Argument 1 is just integer in my application. It cannot exceed more than 100. I strongly feel that the debugger tool which I am using is not pointing to exact line number. It tells me the assignment of

a = atoi(argv[1]) is the line that core dumps. But if I print the value of a and argv1 it is printing correct


0
ozoCommented:
Does it dump core if you remove that line?
What are the lines after that?
0
AggarwalCommented:
can u please post the actual code u r using ritenow ..atleast the related part of the code !!!!
0
jmaheshAuthor Commented:
I am sorry actually the application core dumps on different line and I am now using sun's debug to debug my entire application . I will let u know if I have any probs

Thanks for all your efforts
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.