0

I am trying to write a program that will conjugate a verb in multiple forms.

So I write a function that will allow me to get the part that is kepted and conjugate it. Fixed rule : whole word except last 2 chars.

I am used to OO, and I can't seem to make it work, while it seems a basic program.

I obtain something with weird : here is a screen at the end of the execution, that will be more explicit

enter image description here

I think I missed a little something in my course (probably in the char[] part...), that has a huge impact, but I can't seem to find it. I am opened to all observations on my code, since I am beginning, and I prefer going on a solid basis right now, better that later.

Here is the code

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

void RacineVerbe(char verbeEntier[], char dest[]);
int myStrLen(char *s);
int main()
{
    char *string;
    char *racine;
    string = (char*)malloc(200*sizeof(char));
    racine = (char*)malloc(200*sizeof(char));
    printf("Quel verbe?\n");
    scanf("%s", string);

    RacineVerbe(string, racine);
    printf("%s", racine);
    printf("%sASSE\n", racine);
    printf("%sASSES\n", racine);
    printf("%sAT\n", racine);
    printf("%sASSIONS\n", racine);
    printf("%sASSIEZ\n", racine);
    printf("%sASSENT\n", racine);
    return 0;
}

void RacineVerbe(char verbeEntier[], char dest[]){
int i;
int l = myStrLen(verbeEntier);
for( i = 0; i < l -2 ; i++){
    dest[i] = verbeEntier[i];
}
dest[i+1] = "\0";
}

int myStrLen(char *s){
    int i = 0;
    while(*s++)i++;
    return i;
}
iehrlich
  • 3,552
  • 4
  • 31
  • 42
provençal le breton
  • 1,368
  • 3
  • 22
  • 43

2 Answers2

2

You invoked UB when you wrote dest[i+1] = "\0"; since dest[i+1] expects a char and you assigned "\0" into it, which is a string literal. Replace it with '\0'

Note that string = (char*)malloc(200*sizeof(char)); ---> string = malloc(200); since casting is not needed in malloc in C, and considered bad (as you can see here) and also sizeof(char) is, by definition, 1

CIsForCookies
  • 10,991
  • 7
  • 44
  • 96
  • To be really safe for bugs, this should be used: `string = malloc(sizeof(*string) * 200);` Using dereference syntax inside `sizeof`. – tilz0R Jul 10 '17 at 06:53
  • This is indeed a useless cast on malloc. But useless doesn't make it failed. It was the +1 tough. Thanks. – provençal le breton Jul 10 '17 at 06:53
  • @Zaphod I didn't mean to say the cast caused problems. I just said you could and should remove it. Not that it will make any visible changes in execution – CIsForCookies Jul 10 '17 at 07:06
2

The problem is in you RacineVerbe because you assigned string literal to single character (not a poitner to characters).

In that situation, string literal returns you the address where it is in memory and you assigned to dest[i+i] LSB byte of that address and it may be or may not be visible character. I can assure, your compiler gave you at least warning for that.

Second problem is where you did assignment. You should do it to dest[i] as i was last time incremented in for loop before check failed and therefore i already points to place where 0 should be written.

void RacineVerbe(char verbeEntier[], char dest[]){
    int i;
    int l = myStrLen(verbeEntier);
    for( i = 0; i < l -2 ; i++){
        dest[i] = verbeEntier[i];
    }
    dest[i] = 0; //This line was rewritten.
}

And as already mentioned, try to NOT cast return result of malloc.

tilz0R
  • 6,949
  • 2
  • 21
  • 37