Thread: Assigning a pointer to a variable

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    20

    Assigning a pointer to a variable

    I have a pointer of type char. To the string of characters this pointer points to, I want to assign this to a variable. How do I do this? Thank you.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by rlesko View Post
    I have a pointer of type char...
    A pointer can't be of type char. It can be of type char*. Is that what you mean?

    Quote Originally Posted by rlesko View Post
    To the string of characters this pointer points to, I want to assign this to a variable...
    Your question doesn't really make sense. If you have a char* pointer which "points to" a char array (string), that pointer is a variable, and its value is the address of the first char in the array.

    So, can you state your question more clearly?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    Sorry if I was a bit ambiguous I did not want to give unnecessary information, but it turns out I did not give enough. Allow me to explain.

    I'm working with files on a UNIX server. I have a pointer "char* argv[]" which is a pointer to a string, which is going to reference a pathname of a file directory, which is user inputted. I want to copy the string of characters this pointer points to, to a variable like "char directory[500]". Is this possible? My assignment of "directory=argv[3]" does not work. Let me know if I need to clarify more, thanks for the reply.

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    strcpy is for copying strings. It is usually better to copy strings, rather than pointers to them.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    In C++ you can use the assignment operator = to copy one string (object of the string class) to another. In C a string is just an array of characters (terminated by '\0'), so like any other array you have to copy each element, one at a time -- or use the strcpy() function which does that for you.

    Code:
    #include <string.h>
    // ...
    char directory[500];
    strcpy( directory, argv[3] );
    // ...

  6. #6
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39

    strdup

    From "man strdup" strdup(3) - strdup, strdupa, strndup, strndupa, strdup - save a copy of a string - man 3 strdup
    The strdup() function returns a pointer to a new string which is a
    duplicate of the string s. Memory for the new string is obtained with
    malloc(3), and can be freed with free(3).

    See "man string" for more: Manual for string - man string

    Cprogramming.com FAQ
    Category:Programming Tasks - Rosetta Code
    Main Page - C
    Main Page - clc-wiki

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hellork's suggestion is appropriate because you are using Unix, but note that strdup is otherwise non-standard.
    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

  8. #8
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    Aah yes, I should know better. I forget they are only available on the gcc suite.

    There are several more portable alternatives. See http://rosettacode.org/wiki/Copy_a_string#C.

    Note 1: I think that MAX_PATH should be used instead of 500 in "char directory[500]" if allocating static storage like you were doing for a user-supplied path. Limits.h defines MAX_PATH as 4096 on my Linux box.

    Note 2: The C89-90 draft and C99 draft specify that argv be modifiable. Thus, copying argv is not strictly necessary.:

    " * The parameters argc and argv and the strings pointed to by the argv
    array shall be modifiable by the program, and retain their last-stored
    values between program startup and program termination."

    Notwithstanding, this code works for me if for some reason an exact, dynamically-allocated copy of a string from argv must be had... (Modifying it and want to keep the original value?)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main (int argc, char **argv){
        char *s;
        int sLen;
        if (argc > 1){
            sLen = strlen (argv[1]);
            /* copy sLen characters + terminating NULL */
            /* index starts at 0, so sLen is last character */
            if ((s = malloc(sLen+1))/* no cast please */){
                /* copy first argument into object pointed to by s */
                /* I'd use strncpy & NULL if sLen was < argv[1] */
                strcpy (s, argv[1]);
                /* do something with s ... */
                printf ("%s\n",s        
                /* free the memory for further use */);
                free (s);
            }
            else{
                fprintf (stderr, "error: out of memory!\n");
                return -1;
        }   }
        else{
            printf ("usage: program args\n");
        }
        return 0;
    }
    My C Stuff (GPL):
    PaperDragon
    TML Version 2
    Fix missing brackets with Anchor
    The Nerd Show | Platypus
    Last edited by hellork; 11-15-2010 at 05:27 PM. Reason: always check for NULL pointer on malloc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined reference to.. probably Makefile problem
    By mravenca in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 04:29 AM
  2. How do i know the size of variable pointer
    By unikgila in forum C Programming
    Replies: 10
    Last Post: 10-28-2008, 10:08 AM
  3. How to set pointer for environment variable
    By spiky1 in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2006, 05:19 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM