Re: What is wrong with this code?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: John Carson (donaldquixote_at_datafast.net.au)
Date: 03/24/04


Date: Wed, 24 Mar 2004 18:45:24 +1100


"Robert M." <rmilh@hotmail.com> wrote in message
news:ewxOb8WEEHA.3016@TK2MSFTNGP11.phx.gbl
> #include <string.h>
>
> int main(int argc, char* argv[])
> {
> const char* src[] = {"string"};
> char* dst[] = {"xxxxxx"};
>
> strcpy(dst[0], src[0]); //Access violation here !!!!
>
> return 0;
> }

dst is an array of pointers to char. You are assigning a string literal,
"xxxxxx" to the first element of that array. String literals are "read-only"
regardless of whether you assign them to a char * pointer or a const char *
pointer. You cannot change the contents of the memory that stores string
literals.

-- 
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


Relevant Pages

  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)
  • Re: String literal and const
    ... string literals *cannot* be defined. ... array of type "array N of char" (where N is one more than the number ... char", not "array N of const char". ...
    (comp.lang.c)
  • Re: Difference between Char* ptr and char arrCh []
    ... I have a few queries regarding array of characters using array ... notation and pointer notation. ... Is there a difference in storage of global char* and char* inside ...
    (comp.lang.c)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: Returning pointer to array problem II
    ... Iam trying to make program were I enter string and serach char. ... and funktion prints out witch position char is found this is done if funktion serach_char. ... so far all good what I want do next is: return, from funktion, pointer value to array were positions is stored. ...
    (comp.lang.c)