LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2017, 12:23 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
int main(int argc, char **argv[]) having trouble getting value out of array argv[ ] C programming.


this may to some seem like a stupid question to them that already know how to do this, But this is using pointers so I have no idea how to completely deal with them.

Getting the number off of the command line that is seen as a string? example
Code:
programName 3
get what number was typed in CLI for processing.
break it down? .. OK

If I run just this function I get the results I need to show me my logic in correct.
Code:
#include <stdio.h>

int main(int argc, char **argv[])
{
    int i = 1; 
    // zero based array 
    // then look for greater then zero 
    // zero being the program name

    if ( argc >= 1 )
      printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1]);
   
     /*
     if (argc >= 1 )
     {
       i=argv[argc-1];
	printf("this is i %d\n", i);
      }
      **/
}
results
Code:
userx@slackwhere⚡~/bin/C-files $./a.out 5                                                            
argc 2 argv[argc-1] 5
That work. It is when I umcomment out the next block of code to get the value out of the element in argv[argc-1] and try to assinge it to 'i'

is where i get a cast issue.

Code:
userx@slackwhere⚡~/bin/C-files $gcc Yathee.c
Yathee.c: In function 'main':
Yathee.c:29:9: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
        i=argv[argc-1];
but when I type set it that too does not work.
Code:
userx@slackwhere⚡~/bin/C-files $gcc Yathee.c
Yathee.c: In function 'main':
Yathee.c:28:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
         i=(int)argv[argc];
nor this
Code:
  if (argc >= 1 )
     {
       i=&argv[argc-1];
	printf("this is i %d\n", i); 
      }
which I got off somewhere else from the same error message googled.
nor this
Code:
userx@slackwhere⚡~/bin/C-files $gcc Yathee.c
Yathee.c: In function 'main':
Yathee.c:28:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
         i=(int16_t)argv[argc];
int16_t because It just popped up in Kwrite as a selection, so why not try that too?

I have already googled the this error and what I tried on what I found still did not work.

Quote:
assignment makes integer from pointer without a cast [-Wint-conversion]

Anyways regardless, can someone show me how to get the value out of a pointer( I think is a char) and cast it to int properly?

as I keep getting the same error regardless of what I do.

Last edited by BW-userx; 04-12-2017 at 12:27 PM.
 
Old 04-12-2017, 12:33 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,538

Rep: Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078
Quote:
Originally Posted by BW-userx View Post
Code:
#include <stdio.h>

int main(int argc, char **argv[])
{
That should be either of these:
Code:
int main(int argc, char *argv[])
/*   OR   */
int main(int argc, char **argv)
I find the first declaration ("an array of pointers") more intuitive, but the two are equivalent.

Then your 'printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1])' should work;
 
Old 04-12-2017, 12:53 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by rknichols View Post
That should be either of these:
Code:
int main(int argc, char *argv[])
/*   OR   */
int main(int argc, char **argv)
I find the first declaration ("an array of pointers") more intuitive, but the two are equivalent.

Then your 'printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1])' should work;

ha ha mixing the chili soup with sweet apples just does not taste as good. thanks!

let me put them two to the test.

first
int main(int argc, char **argv)
Code:
#include <stdio.h>

int main(int argc, char **argv)
{
  
  
  int i =1; 
  
   
     if ( argc >= 1 )
      printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1]);
   
     
     if (argc >= 1 )
     {
       i = argv[argc-1];
       printf("this is i %d in argv[argc-1] %s\n", i ,  argv[argc-1]);
      }
      
return 0;
}
results:
Code:
userx@slackwhere⚡~/bin/C-files $gcc array.c
array.c: In function 'main':
array.c:22:10: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
        i = argv[argc-1];
next
int main(int argc, char *argv[])
Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
  
  
  int i =1; 
  
   
     if ( argc >= 1 )
      printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1]);
   
     
     if (argc >= 1 )
     {
       i = argv[argc-1];
	printf("this is i %d in argv[argc-1] %s\n", i ,  argv[argc-1]);
      }
      
return 0;
}
results:
Code:
userx@slackwhere⚡~/bin/C-files $gcc array.c
array.c: In function 'main':
array.c:22:10: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
        i = argv[argc-1];
          ^
Looks like a fail on both accounts to me.

I think you may have missed something.

"Anyways regardless, can someone show me how to get the value out of a pointer( I think is a char) and cast it to int properly?"

Last edited by BW-userx; 04-12-2017 at 12:59 PM.
 
Old 04-12-2017, 12:56 PM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 7
Posts: 3,533

Rep: Reputation: 974Reputation: 974Reputation: 974Reputation: 974Reputation: 974Reputation: 974Reputation: 974Reputation: 974
You can't cast a string to an int. You need to do the conversion by calling a function like atoi.
 
1 members found this post helpful.
Old 04-12-2017, 01:33 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by smallpond View Post
You can't cast a string to an int. You need to do the conversion by calling a function like atoi.
StrToInt what was I thinking... prob wasn't ... thanks.... points given.
 
Old 04-12-2017, 02:01 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Answer:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
  
  
  int i =1; 
  char num[7];
  
     if ( argc >= 1 )
      printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1]);
     
   
     
     if (argc >= 1 )
     {
       strcpy(num, argv[argc-1]);
       i = atoi(num);
	printf("this is i %d in argv[argc-1]%s\n", i ,  argv[argc-1]);
      }
      
return 0;
}
or a direct call without copying string first.
Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  
  
  int i =1; 
  
  
     if ( argc >= 1 )
      printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1]);
     
   
     
     if (argc >= 1 )
      
       i = atoi(argv[argc-1]);
	printf("this is i %d in argv[argc-1]%s\n", i ,  argv[argc-1]);
      }
      
return 0;
}

Last edited by BW-userx; 04-12-2017 at 02:09 PM.
 
Old 04-12-2017, 04:09 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,538

Rep: Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078
Quote:
Originally Posted by BW-userx View Post
int main(int argc, char *argv[])
Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
  
  
  int i =1; 
  
   
     if ( argc >= 1 )
      printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1]);
   
     
     if (argc >= 1 )
     {
       i = argv[argc-1];
	printf("this is i %d in argv[argc-1] %s\n", i ,  argv[argc-1]);
      }
      
return 0;
}
results:
Code:
userx@slackwhere⚡~/bin/C-files $gcc array.c
array.c: In function 'main':
array.c:22:10: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
        i = argv[argc-1];
          ^
Looks like a fail on both accounts to me.
That second "if ..." block was commented out in your original code, so I didn't examine it. What are you trying to assign to "i"? If it's the value of the pointer (i.e., an address in virtual memory), then you just need to do an explicit cast:
Code:
i = (int)argv[argc-1]
That's going to have portability problems on a 32-bit machine where sizeof(int) < sizeof(char *), but it will demonstrate the principle just fine on a 64-bit machine.

If the actual argument passed to the program is a numeric string and you want its value, then you need to use atoi(), as others have suggested.
 
Old 04-12-2017, 04:18 PM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by rknichols View Post
That second "if ..." block was commented out in your original code, so I didn't examine it. What are you trying to assign to "i"? If it's the value of the pointer (i.e., an address in virtual memory), then you just need to do an explicit cast:
Code:
i = (int)argv[argc-1]
That's going to have portability problems on a 32-bit machine where sizeof(int) < sizeof(char *), but it will demonstrate the principle just fine on a 64-bit machine.

If the actual argument passed to the program is a numeric string and you want its value, then you need to use atoi(), as others have suggested.
and that is what I did, but error checking to insure it is a "char" number 1-6 and not a char a-z [anything else] .. will be later on.

and as far as not inspecting that block of commented out code,
It's all cool dude, you must have just missed this is all no big deal, drinks on me..
Code:
That work[s]. It is when I umcomment out the next block of code to get the
value out of the element in argv[argc-1] and try to assinge it to 'i'

is where i get a cast issue.

Last edited by BW-userx; 04-12-2017 at 04:27 PM.
 
Old 04-18-2017, 10:40 PM   #9
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Hopefully this helps:

Code:
| => cat argtest.c
#include <stdio.h>
#include <stdlib.h>

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

	for (int i = 0; i < argc; i++) {
		printf ("Argument: %d Value:%s\n", i, argv[i]);
	}
        exit (0);
}

| => clang -o argtest argtest.c
| =>

test output:
Code:
| => ./argtest 0 1 223 helloWorld! MacOS X
Argument: 0 Value:./argtest
Argument: 1 Value:0
Argument: 2 Value:1
Argument: 3 Value:223
Argument: 4 Value:helloWorld!
Argument: 5 Value:MacOS
Argument: 6 Value:X
| =>

Last edited by Laserbeak; 04-18-2017 at 10:41 PM.
 
Old 04-19-2017, 09:00 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by Laserbeak View Post
Hopefully this helps:
[/code]


test output:
Code:
| => ./argtest 0 1 223 helloWorld! MacOS X
Argument: 0 Value:./argtest
Argument: 1 Value:0
Argument: 2 Value:1
Argument: 3 Value:223
Argument: 4 Value:helloWorld!
Argument: 5 Value:MacOS
Argument: 6 Value:X
| =>
Hey thanks, now I am working on
  1. get input
  2. process
  3. store results
  4. show results
  5. ask for more input
  6. wait for input
  7. repeat

Loop

Thanks
 
Old 04-23-2017, 01:31 AM   #11
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by BW-userx View Post
Hey thanks, now I am working on
  1. get input
  2. process
  3. store results
  4. show results
  5. ask for more input
  6. wait for input
  7. repeat

Loop

Thanks
This should help, but it's not particularly safe and I'll leave the storage part up to you

Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

char *processInput (char *inValue);

int main (int argc, char *argv[]) {
    while (1) {
        printf("Please enter a string [max 1000 chars] (press return to quit): ");
        char *input = (char *)malloc(1001);
        gets(input);
        if (strlen(input) == 0)
            break;
        char *theOutValue = processInput (input);
        printf("%s\n", theOutValue);
        free (input);
        free (theOutValue);
 
    }
    return (0);
}

char *processInput (char *inValue) {
    
    //process by making all caps
    
    char *outValue =  (char *)malloc(sizeof(inValue));
    for (int i = 0; (outValue[i] = toupper(inValue[i])); i++);
    return outValue;
}
 
1 members found this post helpful.
Old 04-23-2017, 02:11 PM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by Laserbeak View Post
This should help, but it's not particularly safe and I'll leave the storage part up to you

Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

char *processInput (char *inValue);

int main (int argc, char *argv[]) {
    while (1) {
        printf("Please enter a string [max 1000 chars] (press return to quit): ");
        char *input = (char *)malloc(1001);
        gets(input);
        if (strlen(input) == 0)
            break;
        char *theOutValue = processInput (input);
        printf("%s\n", theOutValue);
        free (input);
        free (theOutValue);
 
    }
    return (0);
}

char *processInput (char *inValue) {
    
    //process by making all caps
    
    char *outValue =  (char *)malloc(sizeof(inValue));
    for (int i = 0; (outValue[i] = toupper(inValue[i])); i++);
    return outValue;
}
Interesting. Thanks --
the storage shouldn't be too much of an issues if any. it is all of that formatting the output to screen I got a do.
 
Old 04-23-2017, 02:21 PM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
@Laserbeak

just a quick Q: out of curiosity

why is it telling me this?
Code:
userx%slackwhere ⚡ C-files ⚡> gcc malchar.c
malchar.c: In function ‘main’:
malchar.c:12:9: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
         gets(input);
         ^
/tmp/ccFJrg4n.o: In function `main':
malchar.c:(.text+0x39): warning: the `gets' function is dangerous and should not be used.
 
Old 04-23-2017, 02:40 PM   #14
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by BW-userx View Post
@Laserbeak

just a quick Q: out of curiosity

why is it telling me this?
Code:
userx%slackwhere ⚡ C-files ⚡> gcc malchar.c
malchar.c: In function ‘main’:
malchar.c:12:9: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
         gets(input);
         ^
/tmp/ccFJrg4n.o: In function `main':
malchar.c:(.text+0x39): warning: the `gets' function is dangerous and should not be used.


I'm not sure why you're getting the "implicit declaration" message, as long as you #include <stdio.h>. The not safe warning is because gets() doesn't do any error checking as far as the length of the string inputed and the size of string allocated to store it, so it can overrun the buffer, leading to either a crash or worse inserting malicious code into your program.

My compiler simply says "warning: this program uses gets(), which is unsafe."

Last edited by Laserbeak; 04-23-2017 at 02:52 PM.
 
Old 04-24-2017, 03:58 AM   #15
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,150

Rep: Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523
Function 'gets' mustnot be used. Since around 1970. I was two year old then, now I am 49.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] argc and argv confusion atlantis43 Programming 4 09-28-2013 10:42 AM
C programming linux copying argv to wchar_t argv babbab Programming 1 08-09-2012 05:32 AM
[SOLVED] why char *argv[] declaration is ok in main arg, but not in body? hppyhjh Programming 5 09-13-2010 08:36 PM
main(int argc, char **argv) Longinus Programming 4 06-12-2004 07:22 AM
argc argv linuxanswer Programming 8 10-25-2003 07:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration