Rewriting a function using pointers

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Function Pointers
Click For Summary

Discussion Overview

The discussion revolves around rewriting a function that searches for a specific character in a string using pointers, without utilizing square brackets or integer variables. Participants are examining the correctness and structure of the proposed code modifications.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents an original function that uses square brackets and an integer variable, then proposes a modified version that avoids these elements.
  • Another participant suggests that the condition "while (*s != '\0')" can be simplified to "while (*s)", questioning the necessity of checking for the null terminator explicitly.
  • Concerns are raised about the modified function potentially getting stuck in a loop because the pointer 's' is not incremented within the loop.
  • A participant questions the return statement, asking if it should return '&s' since 's' is already a pointer, indicating confusion about pointer usage.
  • One participant suggests copying the control structure of the original function, advocating for the use of a for loop instead of a while loop.
  • Another participant clarifies that the expressions "while (*s)" and "while (*s != '\0')" are equivalent, indicating that the loop will continue as long as 's' points to a character other than the null terminator.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of checking for the null terminator explicitly and the structure of the loop, indicating that multiple competing views remain on how to correctly implement the function.

Contextual Notes

There is uncertainty regarding the proper handling of pointer increments within the loop, as well as the implications of returning '&s' versus 's'.

magnifik
Messages
350
Reaction score
0
I have an original code that searches through "s" for a specific character then stores this into a pointer (otherwise, null). This is the original code:
Code:
const char* findMe(const char s[], char c)
    {
        for (int k = 0; s[k] != 0; k++)
            if (s[k] == c)
                return &s[k];

        return NULL;
    }

I need to rewrite it so it does the same thing but does NOT use any square brackets nor any integer variable. Here is what I have, but I am unsure of whether or not it is entirely correct;
Code:
const char* findMe(const char* s, char c)
   	 {
        while (*s != '\0')
		{
            if (*s == c)
                return &s;
		}
        return NULL;
  	  }
 
Physics news on Phys.org
You don't need "while (*s != '\0')" just use " while (*s)"
You don't increment s you you are only checking the first character - it will get stuck in a loop.
Do you mean to return &s? remember S is already a pointer
 
Why not copy the control structure of the first example? IOW, use a for loop instead of switching to a while loop.

If s is declared as an array of char like this (the 30 is not relevant) -
char s[30];

- you can access the character at index n by array notation (s[n]) or by pointer notation ( *(s + n) ).
 
mgb_phys said:
You don't need "while (*s != '\0')" just use " while (*s)"
You don't increment s you you are only checking the first character - it will get stuck in a loop.
Do you mean to return &s? remember S is already a pointer

i am trying to read in the whole thing, not just the first character... why is it that you only need *s and not *s != '\0'?? i thought you needed that to ensure that it gets to the zero byte/the end of the string
 
The two expressions are equivalent.
while (*s) - means "while *s is any character other than '0'
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 24 ·
Replies
24
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 21 ·
Replies
21
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K