Link to home
Start Free TrialLog in
Avatar of Stephen Kairys
Stephen KairysFlag for United States of America

asked on

C: charpointer for a single-character variable

Hey  everyone,
Need a sanity check here, 99% sure this is OK but need to eliminate the last 1%. :)

I have a character value in a struture
char MyInfo.MyCharField;

I need to pass it to a function that will modify it, so I did the following:
Function header:
void ModifyCharField(char *PtrC)
Call:
ModifyCharField(&MyStructure->MyCharField); // I'm working with a ptr to a structure.

So, first question:
Is the methodology above correct....that is passing the ADDRESS of the char in the call, and using a charpointer (like I would for a string) in the function header?

Now, part two.
I then need to pass PtrC (the charponiter) to a generic input function that accepts a void ptr. I said:
get_input(PtrC);// /I don't use the & to take the address since this is already a ptr.

Second question:
Is THIS methodology (not using the address of PtrC) correct?

Many thanks,
Steve
Avatar of HooKooDooKu
HooKooDooKu

I believe 'MyStructure->MyCharField' equates to '*(MyStructure.MyCharField)', which means you are attempting to dereference a pointer.

Since 'MyCharField' is a simple 'char' (and not a 'char*'), you do not want to use the -> operator because you want the address of a variable rather than trying to dereference a pointer value.

Instead, you simply want '&(MyStruct.MyCharField)' for which there is no simple '->' like substitute operator.
Let's try this again... and lets be more specific with a complete example:
void MyFunc( char* pCh );

struct st
{
  char ch;
};

st MySt;
st* pMySt = &( MySt );

Open in new window

For this code pMySt->ch equates to (*pMySt).ch;

So to call MyFunc, you would need one of the following:
MyFunc( &(pMySt->ch) );
MyFunc( &(MySt.ch) );

Now if the code inside MyFunc() is calling get_input(char*) then
void MyFunc( char* pCh )
{
  get_input( pCh );
}

Open in new window

Avatar of Stephen Kairys

ASKER

Hi HooKooDooKu,
Thanks for your responses!

I think I did not state my question clearly. I think the concept of the -> operator sort of obscured what I was really trying to get at.

So let me start from square one, putting aside the concept of passing a field to a charponiter paramter.

The orignal calling function (which I did not detail) has the signature:
void DoSomething(MY_STRUCTURE *MyStructure);
So, to work with any of its fields, I HAVE to use the -> operator.
e.g.
printf("%s\n", MyStructure->name);
printf("%u\n", MyStructure->age);
printf("%c\n", MyStructure->MyCharField);

This has to be correct...I've always done it this way for over 10 years.
Let's stop here for now, are we in agreement that so far this is OK?
Thanks again.
Steve
SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Evilrix - Thank you for that clarification.
Actually that gets me maybe 1/2% :).....so now we have

MyFunction(char *pCh)

1. So I CAN was the address of a single character (as opposed to a string) to this function?

2.  I then need to pass PtrC (the charponiter) to a generic input function that accepts a void ptr. I said:

get_input(pCh);
where get_input() takes a VOID PTR.
Am I correct to do it this way and not to take the address of pCh?

Thanks again.
Steve
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Evilrix - Thank you. That clears it up quite nicely!
Hookoodooku - thank you for your input as well,.
-Steve