HWZ Forums

Login Register FAQ Mark Forums Read

pointer function help!!

Reply
 
LinkBack Thread Tools
Old 01-03-2016, 01:17 AM   #1
Junior Member
 
Join Date: Feb 2015
Posts: 9
Unhappy pointer function help!!

what is the concept to apply pointer reference to a calling function??
char *stringncpy(char *s1, char *s2, int n); char *sweepSpace1(char *sentence);
char stringncpy(char *s1, char *s2, int n); char sweepSpace1(char *sentence);
696E20727569 is offline   Reply With Quote
Old 02-03-2016, 01:41 AM   #2
Senior Member
 
Join Date: Jun 2014
Posts: 1,033
Question unclear. Try again.
KnightNiwrem is offline   Reply With Quote
Old 03-03-2016, 09:24 AM   #3
Master Member
 
cwchong's Avatar
 
Join Date: Jan 2005
Posts: 4,108
The pointer refers to the return type, ie the first 2 functions retun char pointers;

They are equivalent to
char* functionname()
cwchong is offline   Reply With Quote
Old 07-03-2016, 08:12 PM   #4
Junior Member
 
Join Date: Feb 2015
Posts: 9
what is the concept to apply pointer reference to a calling function??
char *stringncpy(char *s1, char *s2, int n); char *sweepSpace1(char *sentence);
char stringncpy(char *s1, char *s2, int n); char sweepSpace1(char *sentence);
I sought my lecturer about this.. char* is the referencing to a string... so it returns a string rather a char for function.
696E20727569 is offline   Reply With Quote
Old 07-03-2016, 08:46 PM   #5
Master Member
 
Join Date: Jan 2000
Posts: 2,776
I sought my lecturer about this.. char* is the referencing to a string... so it returns a string rather a char for function.
Yes, that's correct.

The top functions return a char. The bottom functions return char*.

And your original question is?
ykgoh is offline   Reply With Quote
Old 07-03-2016, 11:48 PM   #6
Arch-Supremacy Member
 
davidktw's Avatar
 
Join Date: Apr 2010
Posts: 10,059
I sought my lecturer about this.. char* is the referencing to a string... so it returns a string rather a char for function.
For the case of the string related function above, semantically we will understand that functions with return datatype of (char*) are referring to a C string.

In C, a string is just a NUL terminated array of characters. That means the following are strings

char name[] = "ABC"; char emptystring[1]; emptystring[0] = '\0'; char str[3]; str[0] = 'e'; str[1] = 'm'; str[2] = '\0';
However strictly the functions actually return a character pointer. That means it could just be pointer to an array of characters, or pointer to a character. The return datatype of (char*) doesn't necessarily must refers to a string.

You will understand more as you study in-depth into Arrays and Pointers as you progress. For now, for clarity sake, you can just assume these functions are designed to return a C-string.
davidktw is offline   Reply With Quote
Old 08-03-2016, 08:59 AM   #7
Master Member
 
Join Date: Jan 2000
Posts: 2,776
Is your question when do we return a char and when do we return a char*?
ykgoh is offline   Reply With Quote
Old 08-03-2016, 09:26 AM   #8
Senior Member
 
Join Date: Jun 2014
Posts: 1,033
For the case of the string related function above, semantically we will understand that functions with return datatype of (char*) are referring to a C string.

In C, a string is just a NUL terminated array of characters. That means the following are strings

char name[] = "ABC"; char emptystring[1]; emptystring[0] = '\0'; char str[3]; str[0] = 'e'; str[1] = 'm'; str[2] = '\0';
However strictly the functions actually return a character pointer. That means it could just be pointer to an array of characters, or pointer to a character. The return datatype of (char*) doesn't necessarily must refers to a string.

You will understand more as you study in-depth into Arrays and Pointers as you progress. For now, for clarity sake, you can just assume these functions are designed to return a C-string.
I was going to type this, but was too busy and david beat me to it.

While we understand a NULL terminated char array as a String, there are many more representations of a String that are also valid.

So a char*, while commonly used as a String, is more accurately, just a pointer to a char.

Extra:
Some people use char array as byte buffer, so a char pointer would refer to a address to some part of that buffer.

Last edited by KnightNiwrem; 08-03-2016 at 09:29 AM..
KnightNiwrem is offline   Reply With Quote
Reply
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ. Forum members and moderators are responsible for their own posts.

Please refer to our Terms of Service for more information.


Thread Tools

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On