2

I'm new to C and I'm having some troubles with pointers.

In one function (used to print the words) I have a parameter const char *description, which is pointing to a string or char array like "There is a faint outline of a face visible".

In another function I'm going to have a pointer which points to the first character in the description, then move along until it finds a non-space.

char *pointerToFindFirstChar(char *description){
 /* Get my pointer to point to first char in description*/ 
 while (*pointerToFindFirstChar == ' '); 
 pointerToFindFirstChar++;
 return pointer
} 

I am unsure how I can do that though. what I'm trying to achieve is to find the first non space character in a string which is being pointed at by description and store that in another pointer.(hope that makes sense)

Cacho Santa
6,9266 gold badges44 silver badges76 bronze badges
asked Aug 18, 2011 at 23:41
2
  • your function name is misleading. What are you trying to achieve? Commented Aug 18, 2011 at 23:44
  • 1
    @jjc: Please, capitalize the first word of your sentences. It helps people reading it :) Commented Aug 18, 2011 at 23:58

4 Answers 4

1

Try this:

char *pointerToFindFirstChar(char *description)
{
 while(*description == ' ')
 description++;
 return description;
}

Note that checking for the null byte at the end of the string is unnecessary, as when *pointer == '0円', the condition on the while loop while be false and the loop will end anyway.

Getting rid of the ; at the end of the while line is important; otherwise, the loop will have no body and either run 0 times or infinitely (since pointer would never be changed in the loop). If it ran 0 times, then the increment would happen after exiting the loop.

answered Aug 19, 2011 at 4:46
Sign up to request clarification or add additional context in comments.

Comments

1
char *find_first_char(char *desc)
{
 while (*desc == ' ') desc++;
 return desc;
}
answered Aug 18, 2011 at 23:46

7 Comments

ah I want to return a pointer to the first char so I can use it in another function to find the end char of the word
Yeah this should find the first non white space character, if you want alpha characters only you can use isalpha() I believe.
@jjc the function does not modify the original pointer (which was passed in) - it still has the same value which you can pass to another function
Your signature does not match that of the function in the question. The check for the end of the string should be unnecessary, although it isn't in your implementation because by putting a post-increment in the test of the while, you are going one byte farther than you should.
This loop strikes me as backwards. Why not simply while (*desc == ' ') ++desc;? Note that if *desc is zero that condition will be false.
|
0

Just for the record, it is possible to have the post-increment directly in the condition of the loop:

char *pointerToFindFirstChar(char *description)
{
 while (*description++ == ' ');
 return description;
}

In this case you do have an empty loop body because the increment is performed right after the evaluation of the pointer inside the loop condition.

answered Aug 19, 2011 at 5:46

Comments

0

You are currently looking for any different character in your char array. That can also be an exclamation mark or a colon.

Wouldn't it be better to use something like isalnum() or isalpha() ? If you are looking for a Digit (0-9) or Alpha-char (a-z or A-Z) then use isalnum else use isalpha.

char * pointerToFindFirstChar(char * description)
{
 while(*description && !isalnum(*description))
 description++;
 return description;
}

or

char * pointerToFindFirstChar(char * description)
{
 while(*description && !isalpha(*description))
 description++;
 return description;
}

It'll add some overhead though. Also, checking for the end of the char array would be required in this case.

An other option would be is to use isspace() this will check for any white-space character.

See here for these function descriptions: http://www.java2s.com/Code/C/ctype.h/Catalogctype.h.htm

answered Aug 19, 2011 at 9:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.