-3

I'm using the code below to add some "0" chars into my string, but it seems there is a problem and the program will crash. Everything seems logic but I do not know where is the problem?

#include <stdlib.h>
#include <string.h>

int main()
{
    char *Ten; int i=0; Ten = malloc(12);
    Ten="1";
    for (i=0;i<10;i++)
        strcat(Ten,"0");

    printf("%s",Ten);
    return 0;
}
Inside Man
  • 4,194
  • 12
  • 59
  • 119

4 Answers4

5

You declare Ten as a pointer to a string literal. However, you cannot rely on being able to modify a string literal, and thus the program crashes.

To fix this, you can declare Ten as an array instead:

int main()
{
    char Ten[12]="1"; int i=0;

    for (i=0;i<10;i++)
        strcat(Ten,"0");

    printf("%s",Ten);
    return 0;
}

Note that you need 12 bytes; 11 for the characters and one for the terminating NUL character.

Frxstrem
  • 38,761
  • 9
  • 79
  • 119
3

Ten is a string literal and you cannot modify it. Try with array instead

char Ten[12] = "1";
for (i=0;i<10;i++)
    strcat(Ten,"0");

printf("%s",Ten);

notice that I created an array of 12 characters, because there should be room for a termination '\0'.

You actually don't need strcat here, it's just do this

char Ten = malloc(12);
if (Ten != NULL)
{
    Ten[0] = '1';
    for (i = 1 ; i < 11 ; i++)
        Ten[i] = '0';
    Ten[11] = '\0';
    /* Use Ten here, for example printf it. */
    printf("%s",Ten);
    /* You should release memory. */
    free(Ten);
}

or

char Ten = malloc(12);
if (Ten != NULL)
{
    Ten[0] = '1';
    memset(Ten + 1, '0', 10);
    Ten[11] = '\0';
    /* Use Ten here, for example printf it. */
    printf("%s",Ten);
    /* You should release memory. */
    free(Ten);
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • I have edited my Post. Now I'm using malloc to set the size of Ten. but still it is not working, why? – Inside Man Jan 12 '15 at 18:17
  • I mean my own code not yours in my first post. look at it. If I compile it it will wait some seconds and then the app will close without showing the result :( – Inside Man Jan 12 '15 at 18:24
  • @Stranger Because assignment doesn't work as you think, `Ten = malloc(12);` if `malloc` succeeded then `Ten` is pointing to a valid addres in memory, if you do `Ten = "1";` now `Ten` is pointng to a read only address and you've lost reference to the writable one returned by `malloc`. I will fix my answer's second part. – Iharob Al Asimi Jan 12 '15 at 18:31
  • thanks and if you can please check that code via E-Mail ;) Another thing, How to empty the char Ten = malloc(12) ? I need the 12 char string but I need it yo make it empty several times in a loop... – Inside Man Jan 12 '15 at 18:35
  • 1
    what you mean `epmty`? set all values to `0` or to `'0'`?, what you should do is call `free()`. – Iharob Al Asimi Jan 12 '15 at 18:35
  • and as the last question here is this work instead of strcat? "memmove(Ten + 1, "0", 1);" – Inside Man Jan 12 '15 at 18:38
  • no, bad idea... why do you insist on using `strcat`? you could use `memset`. – Iharob Al Asimi Jan 12 '15 at 18:44
  • @Stranger and format your code better and I might take a look, but as it is I don't want to because it's very hard to read it, avoid oneliners and be consistent with indetantion, and use more whitespace, the compiler ignores them, but the human appriciates them. – Iharob Al Asimi Jan 12 '15 at 18:47
  • OK thank you, excuse me can I ask this too? why it is not working? (char *num="1"; if (num=="1") DO;) The Do command should work but it is not :( – Inside Man Jan 12 '15 at 19:08
  • 1
    It should not work, i told you, you are not assigning the content of `"1"` but i'ts address in memory, test this `"1" != "1"` and it should return `true`, because the addresses will not be equal, each one will live in a different place in memory. If you want to compare two strings, you need `strcmp`. In c++ you can overload the `==` operator that's why `num == "1"` could work if `num` is of type `std::string` because there will be an `std::string::operator==(const &...)` implementation, but `"1" == "1"` will be `false` in c++ too. – Iharob Al Asimi Jan 12 '15 at 19:12
2

To quote from strcat manual on linux:

The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs.

Your Ten array is only long enough to store original literal. You need to preallocate memory as long as final desired string.

tumdum
  • 1,981
  • 14
  • 19
1

String literals might be stored in read only section of memory. Any attempt to modify such a literal causes undefined behavior.

To concatenate two strings, the destination must have enough space allocated for the characters to be added and space for '\0'. Change the declaration of Ten to

char Ten[12] = "1";  

and it will work.

haccks
  • 104,019
  • 25
  • 176
  • 264