Pointers and arrays in functions

Salavat 121 Reputation points
2022-01-30T15:30:01.39+00:00

Wrote part of the code from the book by B.Kernigan & D.Ritchie.

#include <cstdio>

using namespace std;

void strcopy(char *s, char *t);

int main()
{
    char *k = (char*)"TEXT1";
    char *g = (char*)"TEXT2";

    printf("%s\n", k);
    printf("%s\n", g);

    strcopy(k, g);

    printf("%s\n", k);
    printf("%s\n", g);

    return 0;
}

void strcopy(char *s, char *t)
{
    while ((*s = *t) != '\0') 
    {
        s++;
        t++;
    }     
}

When I try to compile, I get an error: "Exception thrown: write access violation."
What could be the problem?
I also noticed that many examples do not work directly in Visual Studio - here we can assume that the standard has changed and so on, but this example is a basic work with pointers. The authors do not use the char k = (char)"TEXT"; construct, instead they directly use char *k = "TEXT1"; but it doesn't work!
Are there actual books on the C language (not C++!)?

Developer technologies C++
{count} votes

5 answers

Sort by: Most helpful
  1. Igor Tandetnik 1,116 Reputation points
    2022-01-30T16:21:41.877+00:00

    You are trying to write into memory occupied by a string literal. String literals are placed in a read-only memory segment, to prevent exactly this. Make it

    char k[] = "TEXT1";
    

    or something along these lines - a writable char array.

    1 person found this answer helpful.
    0 comments No comments

  2. Salavat 121 Reputation points
    2022-01-30T18:07:07.787+00:00

    Found a solution: strcopy((char*)&k, (char*)&g);


  3. WayneAKing 4,931 Reputation points
    2022-01-30T18:07:30.18+00:00

    Igor has explained why you get the exception and how to
    overcome it. The following are observations about other
    issues related to your example.

    (1) You said:

    When I try to compile, I get an error:
    "Exception thrown: write access violation."

    That's not exactly accurate. You do not get that error
    when you compile, or link, or build, etc. Your sample
    builds without errors and the exception occurs at
    run time when your compiled code is executed.

    This is not just a semantic quibble, it is important that
    an accurate description of what happens and when it happens
    be given when posting questions, so that an accurate
    diagnosis can be made. In this case it is easy to tell
    that it is not an error that happens at compile time,
    but in many cases it is important to make a distinction.

    I point this out just for your future consideration when
    posting problems, and not to be critical.

    (2) If you are going to try and learn and work with the
    C language - and not the features of C as implemented
    in C++ - then you should always work with the C compiler
    and not with the C++ compiler. The easiest way to do
    that is to ensure that your source code file has an
    extension of .c and not .cpp - the former will use
    the C compiler and rules by default, while the
    latter will use the C++ compiler and rules by default.

    In the sample you posted you have these lines:

    #include <cstdio>
    
    using namespace std;
    

    These will only be accepted in C++ and are not valid
    for C programs. So you probably have a file extension
    of .cpp thus invoking the C++ compiler by default.

    If you change the extension to .c you will need to
    remove the using statement, and change the include
    directive to

    #include <stdio.h>
    
    • Wayne

  4. WayneAKing 4,931 Reputation points
    2022-01-30T18:55:53.423+00:00

    Do you know the literature on pure C? Even if there is
    a hardcore explanation of all the details

    Well, if you aspire to be a "language lawyer" then you
    can spend a considerable amount of your future free
    time reading the official ISO C Language Standard.
    A draft copy of one version of this may be found here:

    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

    A later draft which reflects C11 changes may be found here:

    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

    The official final version of the Standard is not free
    and must be purchased. However, late drafts often are
    very close to the final or even identical.

    Note that these Standards are primarily intended for the
    use of language implementors, such as compiler writers,
    and are not generally suited for learning the language.
    They are always useful for clarifying what implementations
    (compilers) should be doing.

    For most aspiring C programmers, a combination
    of contemporary textbooks and online tutorials
    is usually a smoother and easier path.

    • Wayne
    0 comments No comments

  5. Salavat 121 Reputation points
    2022-02-05T16:18:16.377+00:00

    There are a lot of superfluous things in C++ and C#.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.