0

I have a function as given below:

void deleteNode ( node **my_head, int t_data )
{
 typedef (*my_head) head;    
}

I want to have an alias for the value of the double pointer (*my_head). While compiling I get the following error:

<error: expected '=', ',', ';', 'asm' or '__attribute__' before 'head'>

I do have a workaround but I still need to make this work. Can anyone please help me out with this!!

3
  • error: expected '=', ',', ';', 'asm' or 'attribute' before 'head' Commented Dec 26, 2014 at 6:14
  • You want a new variable? node * head = *my_head? Commented Dec 26, 2014 at 6:24
  • Without using a new variable I want to make an alias for (*my_head). Commented Dec 26, 2014 at 6:25

3 Answers 3

3

You need to alias the type, not the variable name.

typedef node** head;
Sign up to request clarification or add additional context in comments.

4 Comments

This does not solve the purpose for this case as I want to use head as (*my_head) and not as a type alias.
@AnshulTripathi Same problem as others, I too didn't get your question
Thanks for your reply. I want to use (*my_head) thorugh out my function many times. Therefore, to reduce the effort of typing (*my_head) everytime I want an alias which replaces (*my_head) everytime when I use it in the function.
I tried this approach but it doesn't work. The pointer (*my_head) remained unchanged when this function returns i.e. using local variable does not change the pointer (*my_head) when the function returns.
2

syntax for typedef is

typedef <old type name> <new alias>;

so here you should use

typedef node** head

3 Comments

Is there any way out if I want to use head as (*my_head) and not as a type alias?
what exactly you want to do ?
Thanks for your reply. I want to use (*my_head) thorugh out my function many times. Therefore, to reduce the effort of typing (*my_head) everytime I want an alias which replaces (*my_head) everytime when I use it in the function.
0

Kindly notice the nomenclature typedef. It is used in type definition. To be precise, typedef is used to define a new type, not a variable name alias, as you expect.

As per the c99 standard, chapter 6.7.2 type specifiers are

  • void
  • char
  • short
  • int
  • long
  • float
  • double
  • signed
  • unsigned
  • _Bool
  • _Complex
  • _Imaginary
  • struct-or-union-specifier
  • enum-specifier
  • typedef-name

So, you can use typdef for these types. What you're trying is to use typedef with a variable name. That's not allowed.

As others have specified, you have to use typedef in a format

typedef <old type name> <new alias>;

9 Comments

But I still need way out if I want to use head as (*my_head) and not as a type alias?
@AnshulTripathi I'm not sure if i have understood your requirement. Can you give a brief example?
I want to use (*my_head) thorugh out my function many times. Therefore, to reduce the effort of typing (*my_head) everytime I want an alias which replaces (*my_head) everytime when I use it in the function.
@AnshulTripathi well, if it's only about reducing the type effort, and without a new variable, you can try using #define, with a bit of caution with (), {} and ;.
We can't write macro in local scope of a function. I have to again get a overhead of defining this alias macro at the beginning of the file. Is there no way out of making an alias in the function block itself?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.