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.
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.
A pointer can't be of type char. It can be of type char*. Is that what you mean?
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?
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.
strcpy is for copying strings. It is usually better to copy strings, rather than pointers to them.
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] ); // ...
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
hellork's suggestion is appropriate because you are using Unix, but note that strdup is otherwise non-standard.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?)
My C Stuff (GPL):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; }
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