pointer function help!!

696E20727569

Junior Member
Joined
Feb 25, 2015
Messages
9
Reaction score
0
what is the concept to apply pointer reference to a calling function??
Code:
char *stringncpy(char *s1, char *s2, int n);
char *sweepSpace1(char *sentence);

Code:
char stringncpy(char *s1, char *s2, int n);
char sweepSpace1(char *sentence);
 

cwchong

Master Member
Joined
Jan 7, 2005
Messages
4,634
Reaction score
77
The pointer refers to the return type, ie the first 2 functions retun char pointers;

They are equivalent to
char* functionname()
 

696E20727569

Junior Member
Joined
Feb 25, 2015
Messages
9
Reaction score
0
what is the concept to apply pointer reference to a calling function??
Code:
char *stringncpy(char *s1, char *s2, int n);
char *sweepSpace1(char *sentence);

Code:
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.
 

ykgoh

Master Member
Joined
Jan 1, 2000
Messages
2,785
Reaction score
0
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? :s11:
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,391
Reaction score
1,180
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

Code:
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.
 

ykgoh

Master Member
Joined
Jan 1, 2000
Messages
2,785
Reaction score
0
Is your question when do we return a char and when do we return a char*?
 

KnightNiwrem

Senior Member
Joined
Jun 1, 2014
Messages
1,056
Reaction score
0
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

Code:
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. :o

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:
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 Community Guidelines and Standards, Terms of Service and Member T&Cs for more information.
Top