By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

invalid type argument of 'unary*'

P: 1
I always get this error message!
Here's the code:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void)
  5. {
  6.     char *string;
  7.     char length_string = 0;
  8.     char buffer_for_string = 0;
  9.     char *temp_one = malloc(sizeof(char));
  10.     if(temp_one == NULL)
  11.     {
  12.         free(string);
  13.         return -2;
  14.         }
  15.         else
  16.         {
  17.             string = temp_one;
  18.             temp_one = NULL;
  19.             }
  20.  
  21.     while((buffer_for_string = getchar()) != EOF)
  22.     {
  23.         **string = buffer_for_string; // error appears here
  24.         char *temp_two = realloc(*string, sizeof(char)*(length_string+1));
  25.         if(temp_two == NULL)
  26.         {
  27.             free(string);
  28.             return -2;
  29.             }
  30.             else
  31.             {
  32.                 string = temp_two;
  33.                 temp_two = NULL;
  34.                 }
  35.         length_string++;
  36.         }
  37.     getchar();
  38.     return 0;
  39. }
What did I do wrong?
Thanks!
Nov 17 '10 #1

✓ answered by donbock

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:
Expand|Select|Wrap|Line Numbers
  1. int a;     // Declare int.
  2. int *p;    // Declare pointer-to-int.
  3. a = 10;    // Direct reference to 'a'.
  4. p = &a;    // Set 'pa' to point at 'a'.
  5. *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:
Expand|Select|Wrap|Line Numbers
  1. int a;       // Declare int.
  2. int *p;      // Declare pointer-to-int.
  3. int **pp;    // Declare pointer-to-pointer-to-int.
  4. a = 10;      // Direct reference to 'a'.
  5. pp = &p;     // Set 'pp' to point at 'p'.
  6. *pp = &a;    // Indirectly set 'p' to point at 'a'.
  7. **pp = 20;   // [double] Indirect reference to 'a'.
  8. *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.

Share this Question
Share on Google+
2 Replies


Meetee
Expert Mod 100+
P: 931
Why have you taken two pointers there?

Please check this for reference.
Nov 17 '10 #2

Expert 100+
P: 2,400
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:
Expand|Select|Wrap|Line Numbers
  1. int a;     // Declare int.
  2. int *p;    // Declare pointer-to-int.
  3. a = 10;    // Direct reference to 'a'.
  4. p = &a;    // Set 'pa' to point at 'a'.
  5. *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:
Expand|Select|Wrap|Line Numbers
  1. int a;       // Declare int.
  2. int *p;      // Declare pointer-to-int.
  3. int **pp;    // Declare pointer-to-pointer-to-int.
  4. a = 10;      // Direct reference to 'a'.
  5. pp = &p;     // Set 'pp' to point at 'p'.
  6. *pp = &a;    // Indirectly set 'p' to point at 'a'.
  7. **pp = 20;   // [double] Indirect reference to 'a'.
  8. *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.
Nov 17 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.