LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   pointer problem in c. (https://www.linuxquestions.org/questions/programming-9/pointer-problem-in-c-700195/)

kalleanka 01-27-2009 08:08 AM

pointer problem in c.
 
Hi,

I never understod the notation in c and pointers.

This is the problem that I do not understand how to save the return of the function. Shall I make a ptr for it? Is ret saved in ret[32](if yes is it safe)? Can I store the bytes in ret[32] from the returned pointer direcly? Whats the best to do?

I want to understand this because I always get bugs with pointers in c and its a wast of time.

I have:
file one:
...
char ret[32];
...

???? = getMechStatus(ret);


file two:

char * getMechStatus(char ret[])
{
...
...
return ret;
}

kalleanka 01-27-2009 08:15 AM

ok just

getMechStatus(ret);

works fine. Are there any drawbacks?

wje_lq 01-27-2009 11:03 AM

Quote:

Are there any drawbacks?
Not really. You'll want to make a few adjustments:
Code:

file one:
...
char ret[32];
...

getMechStatus(ret);  /* but you'd already made this change */


file two:

void getMechStatus(char ret[])
{
...
...
/* you no longer want this: return ret; */
}

Please keep in mind that you're passing to getMechStatus() not the full 32 bytes, but a pointer to them. As a reminder, most people would probably say
Code:

void getMechStatus(char *ret)
Say this inside getMechStatus():
Code:

printf("%d\n",sizeof(ret));
and you'll see what I mean.

Also, it's up to you to make sure that getMechStatus() stays within the 32 bytes of the array. Obviously, you can't use sizeof() within getMechStatus() to help you. getMechStatus() has no inherent way to test the size of the array.

Also, please keep in mind that the global ret and the parameter ret within getMechStatus() are two separate creatures; the following code would act identically, and most people would probably code two separate names to remind them of this.
Code:

file one:
...
char fred[32];
...

getMechStatus(fred);  /* but you'd already made this change */


file two:

void getMechStatus(char barney[])
{
/* code within this function refers to barney, not fred
...
...
/* you no longer want this: return ret; */
}


irey 01-27-2009 11:47 AM

I would also introduce another parameter:
Code:

void getMechStatus(char ret[], int retLen)
It is common practice in C and C++ to include the length of an array as a parameter to the function that uses it since, as wje_lq showed, you can't use sizeof() for this.

johnsfine 01-27-2009 12:23 PM

Quote:

Originally Posted by kalleanka (Post 3422728)
I never understod the notation in c and pointers.

The way C relates pointers and arrays is convenient, but is one of the more confusing aspects of the language.

Quote:

Is ret saved in ret[32](if yes is it safe)?
I can't even guess what either of those two questions mean.

Quote:

Can I store the bytes in ret[32] from the returned pointer direcly?
Yes.

Quote:

Whats the best to do?
Depends on what you want to accomplish.

Quote:

I have:
All your code looks OK to me. I don't agree wje_lq's apparent objections to it and whether his suggestions would be improvements depends on what you want to do with parts you didn't show.

So mainly it is a question of whether you understand what the code you have actually does.

Quote:

char ret[32];
You globally declare/define a 32 byte chunk of memory.

Quote:

???? = getMechStatus(ret);
You pass the address of that 32 byte chunk of memory to getMechStatus and you get an address back (which happens to be the same address, but this line of code doesn't know that and sometimes that is the point of a design similar to yours).

Quote:

char * getMechStatus(char ret[])
getMechStatus takes and returns a char*
Mainly for readability, the one it takes is a char[] which won't affect the generated code but may make the interface more understandable for someone writing a call to it.

Quote:

return ret;
It returns the same address as it took as an input.

kalleanka 01-28-2009 04:58 AM

Thanks a lot now its more clear.

I did not realize that with getMechStatus(ret) that I just send the pointer. And I did not know the thing with sizeof and the solution void getMechStatus(char ret[], int retLen).

Since I att most use 32 bytes the lengt is not a problem. Its for a coinmech via serialport. But maybe I chould check the length if I want to reuse this code in another project in the future. Just to make it safe.

Thanks again. I have the problem that I sit at home working and I have no one to ask. I do not even know any one personally that programs in c or know c.


All times are GMT -5. The time now is 02:29 AM.