Thread: Is this correct : passing strings?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    26

    Is this correct : passing strings?

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
            char *name = NULL;
    
            name = argv[1];
    
            printf("Name is %s\n", name);
    
            return 0;
    }
    I'm trying to learn weather or not this is a correct way to due the above.
    Is this insecure?

    If it is how can I make it correct?
    I will not know the length of the input.

    WOuld I have to malloc?
    Thank you

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Where are you trying to pass something? You mean into printf? All you need for printf is a pointer to the memory where the string is, which conveniently is exactly what argv[1] is.

    Anyway, the memory behind argv[whatever] is given to you by the OS, more or less; the command-line arguments are placed there before your program starts.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
            if (argc >= 1)
                    printf("Name is &#37;s\n", argv[1]);
            return 0;
    }
    Simplification.
    And added security, in case no argument was passed.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    I am wanting to put argv[1] in my variable name.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    For example.

    I know I can do the following.

    char *name = "myname";
    char buf[10];

    strcpy(buf,name);

    But is my above post doing the same thing?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is not doing the same thing as above.
    You are reassigning a pointer. And you do know what a pointer is, do you not?
    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.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    I'm simply wanting to take in any argument (string) and allocate enough size to a variable so I can store the input ot a variable.
    I don't want to have a set size. (ie: char buf[10]
    Input may be longer than 10.
    So I'd like to have char *buf;
    Then allocated the exact length needed from input to store the input to buf.

    Can you help?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure.
    First, get the size of the input; strlen will help with that.
    Then add +1 to that for the null terminator.
    Then use malloc to allocate the space.
    Then copy it over using strcpy.
    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.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I suppose the first question is "why" -- you already have such a thing; it's called "argv[1]". If for some reason you want a copy, then you can strlen + 1 on what you have to find out how much room you need.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    #include <string.h>     // for strlen and strcpy
    #include <stdlib.h>     // for exit
    #include <stdio.h>     // for printf
    
    int main (int argc, char *argv[]) {
         char *name;
         if (argc < 2) exit (-1);
         if ((name=malloc(strlen(argv[1]+1))) == NULL) exit (-2);
         strcpy(name,argv[1]);
         printf("Name is %s.\n",name);
         free(name);
         return 0;
    }
    This is how you allocate memory for a string to and copy into it. Don't miss out the red bits.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    Thank you very much.

    Please tell me what happens if you do not free(name); ?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It will become a memory leak.
    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.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    How does this effect the system?
    Once the program is killed is there still a memory leak?
    How do you fix the memory leak once this happened?

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    However, after it's freed the string "in" your pointer will be gone, so free it when you are done, or before you reassign it.

    The "memory leak" is gone when the program stops, so in the above example it is sort of unnecessary, but...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It affects the system in that your application eats more and more memory because it failed to free previous memory - it's gone. The application cannot use it again, so it just sits there, eating away at valuable ram while doing nothing.
    Most operating systems free up any resources when an application exits, but not all! The majority here at cboard agree that we think it is good practice to free up anything you allocate - even if it's freed when the application quits.
    And lastly, you can't fix a memory leak once it has happened. That's why it's a memory leak in the first place.
    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. Passing Strings by Reference
    By xshapirox in forum C++ Programming
    Replies: 3
    Last Post: 10-11-2004, 09:35 AM
  2. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  3. correct way of returning strings
    By marsface in forum C++ Programming
    Replies: 5
    Last Post: 06-11-2003, 10:33 AM
  4. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 03:39 PM
  5. passing strings and using strlen
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2001, 10:41 PM