incompatible pointer types

This is a discussion on incompatible pointer types within the C Programming forums, part of the General Programming Boards category; Hi, all-- This program is meant to read a list of names and ages from a file and store them ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    21

    incompatible pointer types

    Hi, all--
    This program is meant to read a list of names and ages from a file and store them in arrays. Unfortunately, I've only gotten as far as dynamically allocating the arrays, and I'm already getting warnings. Here's my code:

    Code:
    void read_file(char (*last_names)[80][20], char (*first_names)[80][20], int (*ages)[80][20], int *count, char filename[80])
    {
    	int i;
    	FILE *fp;
    	fp = fopen(filename, "r");
    	fscanf(fp, "%d", count);
    	
    	for(i = 0; i < *count; i++)
    	{
    		(*last_names)[i] = (char *)malloc(20 * sizeof(char));
    		(*first_names)[i] = (char *)malloc(20 * sizeof(char));
    		(*ages)[i] = (int *)malloc(sizeof(int));
    	}//for i = 0
    	
    }//read_file
    
    void sort(char last_names[80][20], char first_names[80][20], int ages[80])
    {
    
    }//sort
    
    void print_list(char last_names[80][20], char first_names[80][20], int ages[80])
    {
    
    }//print_list
    
    void deallocate(char last_names[80][20], char first_names[80][20], int ages[80])
    {
    
    }//deallocate
    
    int main(int argc, char *argv[])
    {
    	int count = 0, ages[count];
    	char last_names[count][21], first_names[count][21], filename[80];
    	strcpy(filename, argv[1]);
    	read_file(&last_names, &first_names, &ages, &count, filename);
    	sort(last_names, first_names, ages);
    	print_list(last_names, first_names, ages);
    	deallocate(last_names, first_names, ages);
    	return 0;
    	
    }//main
    For the lines in red, I get "error: incompatible types in assignment". For the bits in green, I get "passing argument from incompatible pointer type". If not for the voice of God saying otherwise, I'd say everything checks out. Help?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    char test = 't';
    char * ptr = &test;
    
    *ptr == 't';
    Consider that

    Remember pointers hold addresses, char is not the same as char * (Which is a pointer to a character). malloc() returns a pointer -- an address, you can't store that in a character.

    Perhaps you want an array of pointers instead?

    Also, sizeof(char) is guaranteed to always be 1.
    Last edited by zacs7; 05-21-2008 at 11:21 PM.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    21
    Hm, you're right. Rereading the assignment, I noticed this bit: "Since you will be dynamically allocating the arrays in read_file() the addresses of the original char** of last_names and first_names will be become char*** in read_file()." However, on changing the code as follows:

    Code:
    void read_file(char (**last_names)[80][20], char (**first_names)[80][20], int (**ages)[80][20], int *count, char filename[80])
    {
    	int i;
    	FILE *fp;
    	fp = fopen(filename, "r");
    	fscanf(fp, "&#37;d", count);
    	
    	for(i = 0; i < *count; i++)
    	{
    		(***last_names)[i] = (char *)malloc(20 * sizeof(char));
    		(***first_names)[i] = (char *)malloc(20 * sizeof(char));
    		(***ages)[i] = (int *)malloc(sizeof(int));
    	}//for i = 0
    	
    }
    GCC now gives this warning for the red lines: "assignment makes integer from pointer without a cast". Huh? (***last_names)[i] would be a pointer to a pointer to a pointer to last_names[i], right?

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Okay,
    1. Don't cast malloc()
    2. Pass parameters nicely, char (**last_names)[80][20] is horrible -- not even sure if it's correct
    3. Why don't you pass in a pointer to store the last_names, first_names, etc
    Allocate room for the stuff (ie, how ever many students there is -- as pointers)
    Allocate room for each element

    such:
    Code:
     char ** last_names = NULL;
        size_t i = 0;
    
        last_names = malloc(80 * sizeof(char *));     /* allocate 80 char pointers */
        for(i = 0; i < 80; i++)
        {
            *(last_names + i) = malloc(20);
            /* insert other code here ;) */
        }
    Note: don't hardcode the array sizes, ie '80' or '20'
    Last edited by zacs7; 05-21-2008 at 11:56 PM.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    Quote Originally Posted by quasigreat View Post
    GCC now gives this warning for the red lines: "assignment makes integer from pointer without a cast". Huh? (***last_names)[i] would be a pointer to a pointer to a pointer to last_names[i], right?
    I think zacs7 is right. Due to the complexity, you're way above your head (so to speak). Reduce the complexity of the arguments first, I'd say.
    The warning says that you are assigning an address to a non-pointer, eg:

    Code:
    int x = malloc(1);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incompatible types in assignment.
    By killpoppop in forum C Programming
    Replies: 36
    Last Post: 12-22-2008, 11:08 AM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Pointer types
    By jrfall349 in forum C Programming
    Replies: 8
    Last Post: 04-23-2007, 10:58 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21