Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Copying argc value to a integer variable

Posted on 1999-07-02
14
Medium Priority
?
195 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:jmahesh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +4
14 Comments
 
LVL 2

Accepted Solution

by:
baeg earned 40 total points
ID: 1263152
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
 
LVL 2

Expert Comment

by:cpopin
ID: 1263153
>> 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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1263154
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jmahesh
ID: 1263155
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1263156
I would like to point out that baeg has the two arguments reversed (according to my manual at least).
0
 
LVL 84

Expert Comment

by:ozo
ID: 1263157
baeg had the args backward.  as KangaRoo said, it's
int main(int argc, char* argv[])
0
 
LVL 3

Expert Comment

by:marcjb
ID: 1263158
>> 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
 

Author Comment

by:jmahesh
ID: 1263159
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
 
LVL 3

Expert Comment

by:marcjb
ID: 1263160
How big is argv[1]?
0
 
LVL 3

Expert Comment

by:marcjb
ID: 1263161
I don't mean how big the number is, but how many characters are passed.  What is the command line?
0
 

Author Comment

by:jmahesh
ID: 1263162
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
 
LVL 84

Expert Comment

by:ozo
ID: 1263163
Does it dump core if you remove that line?
What are the lines after that?
0
 
LVL 1

Expert Comment

by:Aggarwal
ID: 1263164
can u please post the actual code u r using ritenow ..atleast the related part of the code !!!!
0
 

Author Comment

by:jmahesh
ID: 1263165
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question