Thread: command line argument - pointer vs value

  1. #1
    Registered User
    Join Date
    Apr 2008

    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
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    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, 02:19 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM