command line argument - pointer vs value

This is a discussion on command line argument - pointer vs value within the C Programming forums, part of the General Programming Boards category; I have a problem with getting the command line argument the command line is: a.out 2008 my code for main ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    1

    command line argument - pointer vs value

    I have a problem with getting the command line argument

    the command line is: a.out 2008


    my code for main is: int main (int argc, char *argv[])

    to return the argument I have printf("Inp para 1: 504d\n", argv[1]);

    when I execute the program I get: Inp para 1: -1073743687

    when I expect: Inp para 1: 2008

    I think somehow I am returning a pointer value instead. Can anyone help me understand this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    You might want to post the actual code you tested with.

    Anyway, if you are using the %d format specifier, then you need to convert the string to an int. If not, just use the %s format specifier and print the string as-is.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    The command line is passed as a string and not integers and the %d tells printf to print an integer which results in the garbage data you see.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-21-2008, 02:52 PM
  2. Replies: 4
    Last Post: 04-21-2008, 12:15 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21