billpcs
billpcs

Reputation: 633

Manipulating pointers and functions

Although I have been learning C for 4 months now I have a difficulty in manipulating pointers especially when having to pass them as function parameters. I have the below small code:

int getData(){

    int i = -1  , people ;
    char **clientNames ; 
    printf("Enter the number of people you want to save: ");
    scanf("%d" , &people ) ; 
    clientNames = malloc( people * sizeof(char *) ) ;
    do{
        i++ ;
        char temp[60] ;
        printf("Enter Full Name: ");
        scanf("%s" , temp ) ; 
        clientNames[i] = malloc( sizeof(temp) ) ; 
        strcpy( clientNames[i] , temp )  ;
        printf("Saved as: %s\n", clientNames[i]  );

    }while ( i != people - 1) ; 

    clientNames[i+1] = malloc( sizeof("\0") ) ;
    clientNames[i+1] = "\0" ; 

    return clientNames ; 
}

int main(){
    int i = 0 ; 
    char **C;
    C = getData() ;   
    printf("Stoped saving.\n");
    for (i=0 ; strcmp(C[i] , "\0")  ; i++ ){
        printf("%s\n", C[i] );
    }   
    return 1 ; 
}

So I have 2 Questions:

1) When the above code is compiled (gcc) I get the warning: warning: assignment makes pointer from integer without a cast [enabled by default] . What can be done in order for this warning to vanish? I tried changing the return type of getData() but still , the same warning.

2) How can this code be modified so that the char C that is created in main() is **passed as a parameter in getData() ? Then getData should be able to change its content and return the **C.

This is the value of pointers after all , right? :)

Upvotes: 0

Views: 94

Answers (2)

macfij
macfij

Reputation: 3209

1) When the above code is compiled (gcc) I get the warning: warning: assignment makes pointer from integer without a cast [enabled by default] . What can be done in order for this warning to vanish? I tried changing the return type of getData() but still , the same warning.

change

int getData(){

to

char** getData(){

since you are returning a char** from function, not int. after all you should also free the memory that you've allocated.

2) How can this code be modified so that the char C that is created in main() is **passed as a parameter in getData() ? Then getData should be able to change its content and return the **C.

in C, arguments are passed by value. in order to pass a pointer from main to your function you need to pass an address of it. so, getData() would expected a pointer to C. within function, to operate on C variable you would have to dereference it. if you would pass C to getData() there's no need to return it. so, it would go like:

void getData(char*** C){
    char** clientNames = *C;
    ...

and in main():

...
char** C;
getData(&C);
...

Upvotes: 1

barak manos
barak manos

Reputation: 30136

I'd tell you which lines you should change in order to work out your code, but you pretty much need to change everything, so read the code below and try to follow up with the differences:

char** getData()
{
    int i, people;
    char** clientNames;
    char temp[60];

    printf("Enter the number of people you want to save: ");
    scanf("%d", &people);
    clientNames = malloc((people+1)*sizeof(char*));

    for (int i=0; i<people; i++)
    {
        printf("Enter Full Name: ");
        scanf("%s", temp);
        clientNames[i] = malloc(strlen(temp)+1);
        strcpy(clientNames[i], temp);
        printf("Saved as: %s\n", clientNames[i]);
    }

    clientNames[people] = NULL;
    return clientNames;
}

int main()
{
    int i;
    char** C = getData();
    printf("Stoped saving.\n");
    for (i=0; C[i]!=NULL; i++)
    {
        printf("%s\n", C[i]);
        free(C[i]);
    }
    free(C);
    return 0;
}

I left out the assertions required after malloc and before free, but you generally need them as well...

Upvotes: 3

Related Questions