0

Hello I try to read a string from main and then parse it to a function and find its size but my code doesn't works can you help me?

 //main
         int size=10;
            char *string= (char*) malloc (sizeof(char)*15);

            scanf("%s",string);
            findAllReplacements(NULL,10,string);

//at findAllReplacements

void findAllReplacements(nameInfoT* names,int size,char* expression){
    int ssize=strlen(expression);
    printf("%stringsize:%d\n",ssize);

The program crashes at strlen. What am I doing wrong? I give as input "astring"

The size argument has nothing to do with the size of the string;

JmRag
  • 1,443
  • 7
  • 19
  • 56
  • Is 14 characters enough for your input? – V-X Jan 05 '14 at 19:04
  • 1
    `sizeof(char)` is `1`. – Carl Norum Jan 05 '14 at 19:04
  • 3
    [Don't cast the return value of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858)! –  Jan 05 '14 at 19:05
  • 1
    `"%stringsize:%d\n"` ---> `"stringsize:%d\n"` – BLUEPIXY Jan 05 '14 at 19:06
  • Of course cast the value to malloc(), a non sequitur in any case. Looks like you tell printf() you are going to pass it a string, but then fail to do so. – Ron Burk Jan 05 '14 at 19:06
  • @V-X of course I give as input the "astring" a.k.a 7 chars; – JmRag Jan 05 '14 at 19:07
  • 1
    @BLUEPIXY Actually I was pretty dumb, the program chrashed at the printf. I need to be more careful. – JmRag Jan 05 '14 at 19:09
  • @H2CO3: OP probably wanted to make portable code.:) @.BLUEPIXY: You hit the nail on the head. – V-X Jan 05 '14 at 19:11
  • @V-X Casting malloc doesn't improve portability. All conforming C compilers need to accept the code without the cast. –  Jan 05 '14 at 19:20
  • 3
    This question appears to be off-topic because it is about a typo. –  Jan 05 '14 at 19:21
  • i ment c/c++ portability... – V-X Jan 05 '14 at 19:26
  • @V-X There's no such language as "C/C++". There are two different languages called "C" and "C++", respectively, and compilers for the one are not suited for compiling programs written in the other. –  Jan 05 '14 at 19:29

1 Answers1

2

The program crashes at strlen.

Possibly because the string that you enter from stdin is larger than the size of the allocated memory.

Few more issues

printf("%stringsize:%d\n",ssize);

Aparently, %s is interpreted as if, you would be passing a string as a vararg. So per your format string, your printf excepts two arguments of type string and integer consecutively, , but instead ended up passing a singleton integer. You should have coded as

printf("%%stringsize:%d\n",ssize);

In C, casting the return value of malloc is superfluous.

Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • Yes I want's cautious enough and I typed another % by mistake. So it becomes a really fool question. – JmRag Jan 05 '14 at 19:12
  • @H2CO3: I won;t call explicit cast in `C` an error, but rather unnecessary and a bad practise. So, I am not agreeing with your edit. – Abhijit Jan 05 '14 at 19:43
  • @Abhijit It **is** an error (just read [this](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858)). What do you call a doing that results in a lot of harm and no benefit? I call it either a crime (which this isn't) or an error/mistake. –  Jan 05 '14 at 19:45
  • @H2CO3: Refer the first statement of unwind. He says it unnecessary (superfluous). To me an error would directly translate to a compiler or runtime error, which in this case would rather not be true. I disagree in your use of erroneous, but instead if you have edited it to mistake, I might have acceded. – Abhijit Jan 05 '14 at 19:47
  • @Abhijit "compiler error" and "runtime error" are subsets of errors. "Design errors" and "usage errors" are yet some other subsets. –  Jan 05 '14 at 19:48
  • @H2CO3: Then we have different school of thoughts. I would rather agree to call the later as programming mistakes rather than errors. – Abhijit Jan 05 '14 at 19:50