0

I use CodeBlocks for Windows as my IDE. In this program, I am trying to copy two different strings (string[8][0] with string[9][0]), and I can't, even though they have the exact same length. I don't understand why the program isn't working because, when I used two different strings with the same length in an exercise the program, worked.

#include <stdio.h>
#include <string.h>

int main()
{
    char* string[10][10];
    int i;
    string[8][0] = "ooo";
    string[9][0] = "uuu";
    puts(string[8][0]);
    puts(string[9][0]);
    printf("%d %d", strlen(string[8][0]), strlen(string[9][0]));//This line is just to make sure that both strings have the same lenght
    strcpy(string[8][0], string[9][0]);//I want to copy the content of the string "string[9][0]" to the string "string[8][0]" and replace what was on that string
    puts(string[8][0]);
    puts(string[9][0]);
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
KazzioBots
  • 37
  • 5
  • Does this answer your question? [What should happen, when we try to modify a string constant?](https://stackoverflow.com/questions/27211884/what-should-happen-when-we-try-to-modify-a-string-constant) – manveti Apr 16 '20 at 17:59
  • regarding `string[8][0] = "ooo";` and simolar statements: the code is trying to force a string into a single char, Suggest: `strcpy( string[8], "ooo" ); – user3629249 Apr 17 '20 at 21:12

2 Answers2

2

What you are seeing is undefined behaviour1. Why? Because the line string[8][0] = "ooo"; assigns, to the pointer at string[8][0], the address of a constant string literal. Then, when you later call strcpy(string[8][0], string[9][0]);, you are trying to write to that constant data.

A 'quick fix' to make your program work is to copy the string literal into a non-constant char array, like so:

int main()
{
    char* string[10][10];
    int i;
    //    string[8][0] = "ooo";
    char buffer[4] = "ooo"; // This line COPIES the literal into the non-const array
    string[8][0] = buffer;  // ... and here we give the pointer that array's address
    string[9][0] = "uuu";
    puts(string[8][0]);
    puts(string[9][0]); // Note, I've added a newline below to make the output tidier!
    printf("%d %d\n", strlen(string[8][0]), strlen(string[9][0]));//This line is just to make sure that both strings have the same lenght
    strcpy(string[8][0], string[9][0]);//I want to copy the content of the string "string[9][0]" to the string "string[8][0]" and replace what was on that string
    puts(string[8][0]);
    puts(string[9][0]);
    return 0;
}

1 The undefined behaviour can cause any number of things to happen! On some platforms, the program may silently 'ignore' your attempt to overwrite the constant data; on my platform, the program crashes (as it appears to on yours).

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Thanks for the response! This programm was only an example, and in my actual programm the content of the array string[8][0] is random, and that's why I can't just use the char buffer with always the same value. Is there another way to fix this problem? – KazzioBots Apr 16 '20 at 20:59
  • The only thing you have to ensure is any pointer passed as the *first* argument to the `strcpy` function (that is, the *destination* string) does not point to a constant (literal) string. How to check for (or prevent) this in your real-world program depends on many factors; maybe you need to re-think your design? – Adrian Mole Apr 16 '20 at 21:27
  • How do I acess a specific char of the string[9][0]? I want to acess only the first 'u' for example. – KazzioBots Apr 17 '20 at 10:25
0

regarding

string[8][0] = "ooo"; 

and similar statements:

the code is copying a point to read-only memory

And remember that read-only memory cannot be changed by the program.

Suggest modifying the code so the string(s) are actually placed in the array, similar to.

char string[10][10];
...
strcpy( string[0], "ooo" );

regarding:

printf("%d %d\n", strlen(string[8][0]), strlen(string[9][0]));  

the function: strlen() returns a size_t, not an int so the output format conversion' specifiers should be %zu not %d

If you compiler did not warn you about the problems in the code, then enable the warnings then fix those warnings.

For gcc, at a minimum use: -Wall -Wextra -Wconversion -pedantic -std=gnu11

Note the other compilers use different options to produce the same results

user3629249
  • 16,402
  • 1
  • 16
  • 17