1

I am doing my homework, the problem is as follows:

  1. accept words separated by commas.
  2. extracted each word and reverse them.
  3. put them back in order.

for example, if I enter "apple,egg", I get "elppa,gge"

So far I've completed most of the program, the program works well when I enter less than four words, but with more than four words such as "ybur,etaga,etiluzal,iluzal sipal,etihcalam" the program doesn't work and it shows me return value 3221225477. I just learned how to use dynamical memory allocation so I think it may result from the fact that I did not use it properly, if that's true, please correct me.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    int c=1,i,s;
    char a[1000];
    fgets(a,1000,stdin);
    for(i=0;i<strlen(a);i++){
        if(a[i]=='\n'){
            a[i]='\0';
        }
    } 
    for(i=0;i<strlen(a);i++){
        if(a[i]==','){
            c++;
        }
    }
    char **b;
    b=(char**)malloc(sizeof(char)*c); 
    for(i=0;i<c;i++){
        b[i]=(char*)malloc(sizeof(char)*100);
    }
    strcpy(b[0],strtok(a,","));
    for(i=1;i<c;i++){
        strcpy(b[i],strtok(NULL,","));
    }
    char **d;
    d=(char**)malloc(sizeof(char*)*c);
    for(i=0;i<c;i++){
        d[i]=(char*)malloc(sizeof(char)*strlen(b[i]));
        for(s=0;s<strlen(b[i]);s++){
            d[i][s]=b[i][strlen(b[i])-s-1];
        }
    }
    printf("%s",d[0]);
    for(i=1;i<c;i++){
        printf(",%s",d[i]);
    }
    for(i=0;i<c;i++){
        free(b[i]);
        free(d[i]);
    }
    free(b);
    free(d);
    return 0;
}

I hope that the program works no matter what words I enter.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
FunCry
  • 23
  • 4
  • You could be accessing a memory location that you shouldnt be which is why you are getting a memory address instead of a string. – binjamin Nov 04 '19 at 05:48
  • 2
    3221225477 is 0xC0000005. That's a well known Windows error code – selbie Nov 04 '19 at 06:07
  • regarding: `for(i=0;i – user3629249 Nov 05 '19 at 02:15
  • OT: regarding statements like: `b=(char**)malloc(sizeof(char)*c);` 1) in C, the returned type is `void*` which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc. Suggest removing that cast. 2) the expression: `sizeof( char )` is defined in the c standard as 1. Multiplying anything by 1 has no effect and just clutters the code. Suggest removing that expression. 3) always check (!=NULL) the returned value to assure the operation was successful. if not successful, then call `perror( "malloc failed" )` – user3629249 Nov 05 '19 at 02:20
  • @user3629249 thank you very much, I'm new to programming and thus are not familiar with that kind of knowledge. where can I read things like that? – FunCry Nov 05 '19 at 07:24
  • The easiest way to learn the details of the C library functions that you use is to read/understand the MAN page for each of those functions and then always check for an error when calling any of the C library functions – user3629249 Nov 05 '19 at 16:30

1 Answers1

1
b=(char**)malloc(sizeof(char)*c); 

should be

b = malloc(sizeof(char *) * c); 
                       ^--------------(sizeof pointer)

As of now you are only allocating sizeof char * c to char ** as it should be sizeof pointer * c.

Also you don't need to cast the malloc return.

kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • I didn't notice that I made such a stupid mistake....Thank you. – FunCry Nov 04 '19 at 05:55
  • @FunCry That's okay. – kiran Biradar Nov 04 '19 at 05:55
  • And I want to know why I don't need to cast the malloc return. Is it because that I return it at the end of the program? – FunCry Nov 04 '19 at 05:57
  • @FunCry Please read https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – kiran Biradar Nov 04 '19 at 05:58
  • @FunCry: A quiet save pattern to always get the right size is to do: `char ** b = malloc(c * sizeof *b);` No specification of a type is needed. Please note that `sizeof` is an operator and not a function, so if used with a variable there is *no* need to enclose the variable into parenthesis. – alk Nov 04 '19 at 15:43