0

What's wrong with this code?:

int main(int argc, char ** argv) {
    char strs[] = "What will be printed?";
    char *str1;
    char *str2;

    strs[5] = '\0';
    str1 = strs;
    strcpy(str2, str1);

    printf("%s\n", str2);

    return 1;
}

I want it to print "What", instead i get segmentation fault.

I believe it has something to do with the strcpy(str2, str1);, but what is the explanation for that? The signature of strcpy is char* strcpy(char* destination, const char* source); and that's exactly what i did.

Could you explain that to me?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
ryden
  • 189
  • 6
  • 3
    Ask yourself what `str2` points to when `strcpy(str2, str1);` is invoked? Perhaps read [strcpy](https://en.cppreference.com/w/c/string/byte/strcpy) and consider whether the `str2` argument complies with the requirements for a successful execution of that function. – WhozCraig Jan 27 '22 at 10:54
  • You haven't initialized `str2`. So you are pretty much trying to copy data on some memory that can be anything. – Bes Dollma Jan 27 '22 at 10:59
  • @BesDollma But if i'll initialize ```str2```, then i won't be able to change it, because it's immutable. The only thing that works is when i declare ```char str2[20]``` instead of ```char *str2;```. But i wonder if it's still possible when i use ```char *str2``` and not the other one. – ryden Jan 27 '22 at 11:07
  • Make it an array `str2[20]` or allocate some memory `str2 = malloc(strlen(str1)+1);`. Also check `str2` after malloc (might be `NULL`). – Tudor Jan 27 '22 at 11:12
  • Huh? Who says `str2` is immutable?? – Jabberwocky Jan 27 '22 at 11:26
  • I've used my canonical dupe for "help I'm storing things into uninitialized pointers" so many times today that I fear I'll wear out the memory cells on the SO server where that post is stored... – Lundin Jan 27 '22 at 11:44
  • @ryden who told you it is immutable? This is not Python. In C you can change any bit that you want anywhere that you want as long as you know what you are doing. – Bes Dollma Jan 27 '22 at 12:22
  • In C you can regard _string literals_ as immutable, but not strings in general. See https://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-char-s-initialized-with-a. – Lundin Jan 27 '22 at 13:12
  • That's what i meant: ```char *str2 = "initialized";``` Here, it's still not working, even though ```str2``` is already initialized. Thats because it's immutable, don't it? – ryden Jan 27 '22 at 13:53

2 Answers2

1

Your destination string is not initialized - it has no memory reserved for itself. An attempt to write to it causes invalid memory access (you're trying to overwrite something completely random and unplanned) and is followed by a segmentation fault.

One clean way to initialize a string is to define a global macro variable that just sets the largest size of strings you plan on using in your code,

#define MAXBUF 100

Then in your main you can simply write:

char str2[MAXBUF];

And your program will work. Alternative is to use dynamic memory allocation which you will likely learn about soon.

atru
  • 4,699
  • 2
  • 18
  • 19
0
#include <stdio.h>
#include <string.h>

int main() {
    char *src = "What will be printed?";
    char dest[100];

    int START = 0;  // start of copy
    int LENGTH = 5; // length of copy

    // copies up to LENGTH characters from the string pointed to, by src to dest. In a case where the length of src is less than that of LENGTH, the remainder of dest will be padded with null bytes.
    strncpy(dest, &src[START], LENGTH); 

    printf("%s\n", dest);

    return 0;
}
Ihdina
  • 950
  • 6
  • 21