• C

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
LVL 4
Stephen KairysTechnical Writer - ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HooKooDooKuCommented:
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.
0
HooKooDooKuCommented:
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

0
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
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
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

evilrixSenior Software Engineer (Avast)Commented:
Assuming MyStructure is a pointer to a structure containing MyCharField then the following:

&MyStructure->MyCharField

will get the address of MyCharField, which will be passed to the function.

Put another way, you can now claim your additional 1% :)
0
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
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
0
evilrixSenior Software Engineer (Avast)Commented:
1. A string is nothing more than an array of chars, you can think of a char as an array of chars that has a length of one item. In other words, if a function takes a char pointer you can pass it either a string or the address of a char - from a pointer point of view they the same thing.

2. Any pointer will automatically decompose to a void pointer so if you have a char pointer you can pass it to a function that expects a void pointer.

>> Am I correct to do it this way and not to take the address of pCh?
Yes.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Evilrix - Thank you. That clears it up quite nicely!
Hookoodooku - thank you for your input as well,.
-Steve
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.