invalid type argument of 'unary*'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stephanie Smith
    New Member
    • Nov 2010
    • 1

    #1

    invalid type argument of 'unary*'

    I always get this error message!
    Here's the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char *string;
    	char length_string = 0;
    	char buffer_for_string = 0;
    	char *temp_one = malloc(sizeof(char));
    	if(temp_one == NULL)
    	{
    		free(string);
    		return -2;
    		}
    		else
    		{
    			string = temp_one;
    			temp_one = NULL;
    			}
    	
    	while((buffer_for_string = getchar()) != EOF)
    	{
    		**string = buffer_for_string; // error appears here
    		char *temp_two = realloc(*string, sizeof(char)*(length_string+1));
    		if(temp_two == NULL)
    		{
    			free(string);
    			return -2;
    			}
    			else
    			{
    				string = temp_two;
    				temp_two = NULL;
    				}
    		length_string++;
    		}
    	getchar();
    	return 0;
    }
    What did I do wrong?
    Thanks!
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Why have you taken two pointers there?

    Please check this for reference.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      A variable is referenced directly via its name. A variable is referenced indirectly via some other variable that contains a pointer to the first variable. Indirect references are accomplished through the dereference operator (asterisk, *). For example:
      Code:
      int a;     // Declare int.
      int *p;    // Declare pointer-to-int.
      a = 10;    // Direct reference to 'a'.
      p = &a;    // Set 'pa' to point at 'a'.
      *p = 20;   // Indirect reference to 'a'.
      Line 5 is said to dereference pointer 'p'.

      The compiler supports more levels of indirection than most people should use. It is uncommon (but not unusual) for me to use two levels of indirection; but I rarely use three levels of indirection and I don't remember ever using four. For example:
      Code:
      int a;       // Declare int.
      int *p;      // Declare pointer-to-int.
      int **pp;    // Declare pointer-to-pointer-to-int.
      a = 10;      // Direct reference to 'a'.
      pp = &p;     // Set 'pp' to point at 'p'.
      *pp = &a;    // Indirectly set 'p' to point at 'a'.
      **pp = 20;   // [double] Indirect reference to 'a'.
      *p = 30;     // Indirect reference to 'a'.
      Lines 6 and 7 are said to dereference pointer 'pp'.
      Line 8 is said to dereference pointer 'p'.

      Lines 23 and 24 of your original program do not use the dereference operator properly.

      Note: this post does not show all of the ways to declare and dereference pointers.

      Comment

      Working...