LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-27-2009, 08:08 AM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 547

Rep: Reputation: 38
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;
}
 
Old 01-27-2009, 08:15 AM   #2
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 547

Original Poster
Rep: Reputation: 38
ok just

getMechStatus(ret);

works fine. Are there any drawbacks?
 
Old 01-27-2009, 11:03 AM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 808

Rep: Reputation: 178Reputation: 178
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; */
}
 
Old 01-27-2009, 11:47 AM   #4
irey
Member
 
Registered: Jun 2008
Location: Torino, Italy
Posts: 65

Rep: Reputation: 17
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.
 
Old 01-27-2009, 12:23 PM   #5
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,051

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by kalleanka View Post
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.
 
Old 01-28-2009, 04:58 AM   #6
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 547

Original Poster
Rep: Reputation: 38
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
a simple pointer problem kapsikum Programming 2 03-09-2005 11:05 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
Mouse pointer problem thort Mandriva 1 08-03-2004 04:15 AM
Problem about pointer in C akin81 Programming 5 04-03-2004 06:44 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration