[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Copying argc value to a integer variable

Posted on 1999-07-02
14
Medium Priority
?
198 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
  • 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
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

 

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 85

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 85

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

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

591 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