0

I have a problem with the set_ccs function. I cannot take elements from user. How can i fix that?

int main(){

    char *ccs;

    *ccs =(char*)malloc(sizeof(char) * 80);//i have to use dynamic memory allocation    
     printf("Enter CCS: ");
     set_ccs(&ccs); 
     free(ccs);
  return 0;
}

int set_ccs(char **ccs){

    int i = 0;

    scanf("%s",*ccs);//Is it better to use fgets? Because scanf seems to count 'enter'

    while(*ccs!='\0'){
       ccs++;
       i++;
    }

    printf("Length of sequence : %d\n",i);//It always return 3
    printf("%s",ccs); //with weird elements
 return i;
}

Thanks already.

dgknrsln
  • 13
  • 3
  • 2
    It's in your best interest not to cast the return value of `malloc` immediatelly like that. – Shark Apr 20 '16 at 08:06
  • Send single pointer, not double pointer when the `malloc` is in the calling function. – i486 Apr 20 '16 at 08:07
  • Please give a minimal, compilable example. We can not help without enough informations. – Boiethios Apr 20 '16 at 08:08
  • 2
    And in `main` what is `ccs` and how is it initialized? Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. And when you edit your question, please include things like input, and expected and actual output. – Some programmer dude Apr 20 '16 at 08:09
  • Oh and there are also other problems, like you using `*ccs` as a single character, or `ccs` as a string, none of which are right. – Some programmer dude Apr 20 '16 at 08:11

2 Answers2

0

This:

char *ccs;

*ccs =(char*)malloc(sizeof(char) * 80);

Is wrong, and there is no sane compiler that will accept it. You're shoving a pointer into a char, and it certainly won't fit.

It should just be:

ccs = malloc(80);

There's no need to scale by sizeof (char), that is always 1.

Also, please don't cast the return value of malloc() in C.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • And `int set_ccs(char **ccs){` should be `int set_ccs(char *ccs){`, and `scanf("%s",*ccs);` should be `scanf("%s",ccs);` ... a lot of problems – David Ranieri Apr 20 '16 at 08:24
  • @AlterMann int set_ccs(char **ccs) is a requirement for my project. Is it wrong or is it a option using like that? Also there is a [Error] invalid conversion from 'void*' to 'char*' [-fpermissive] when i write like ccs = malloc(80) but it is okay with ccs =(char*)malloc(sizeof(char) * 80); – dgknrsln Apr 20 '16 at 08:57
  • Yes, it is wrong because you are comparing a `char` with a pointer to `char` in the `while` loop, same for your `printf`, you are printing a pointer to pointer to `char` using a pointer to `char` (`%s`) specifier. – David Ranieri Apr 20 '16 at 09:03
0

In addition to unwinds answer that you should just use

char *ccs;
ccs = malloc(80);

You should make the function set_ccs() accept a pointer:

int set_ccs(char *ccs)

And call it like this from your main:

set_ccs(css);

Then in your function you can use scanf() like so:

scanf("%s", css);

Now you if you want to check for '\0', it is good practice to initialize your "string" to 0 before you use it. You can do that with calloc(80) instead of malloc(80).

If you need to have the pointer to pointer (char **ccs), you have to make a double pointer in your main, check this code:

int main(){

    char *ccs;
    char **ccs2; //a pointer to a pointer

    ccs = calloc(80); //i have to use dynamic memory allocation
    ccs2 = &ccs; //pass the address of the pointer to the double pointer

    printf("Enter CCS: ");
    set_ccs(ccs2); //pass the double pointer
    free(ccs);
    return 0;
}

int set_ccs(char **ccs){

    int i = 0;

    scanf("%s", *ccs);
    char *c = *ccs; //copy to make increments

    while(*c != '\0'){
        c++;
        i++;
    }

    printf("Length of sequence : %d\n", i);
    printf("%s", *ccs);
    return i;
}
moffeltje
  • 4,521
  • 4
  • 33
  • 57
  • int set_ccs(char **ccs) is a requirement for my project. Is it wrong or is it a option using like that? – dgknrsln Apr 20 '16 at 09:04
  • I have 2 more problem now. First, i get error when i use 'ccs = calloc(80)' but it is okay with 'ccs = (char*)malloc(sizeof(char) * 80)' . It is working now but do you think something is wrong? Secondly, when i enter char sequence with space e.g. **program coding** , the code always take the part before the space. What should i do for taking all of the sequence? fgets? – dgknrsln Apr 23 '16 at 08:44