Rewriting a function using pointers

In summary, the conversation discusses rewriting original code that searches for a specific character in a string and stores it into a pointer. The new code uses a while loop and pointer notation instead of square brackets and an integer variable. The conversation also clarifies the purpose of using "while (*s != '\0')" and suggests using "while (*s)" as a simpler alternative.
  • #1
magnifik
360
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
  • #2
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
 
  • #3
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) ).
 
  • #4
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
 
  • #5
The two expressions are equivalent.
while (*s) - means "while *s is any character other than '0'
 

1. What are pointers in programming?

Pointers in programming are variables that store the memory address of another variable. They are used to indirectly access and manipulate data stored in a computer's memory.

2. Why would I want to rewrite a function using pointers?

Rewriting a function using pointers can improve efficiency and reduce the amount of code needed. It also allows for more control over memory management, as pointers can be used to dynamically allocate and deallocate memory.

3. How do I declare a pointer in a function?

To declare a pointer in a function, use an asterisk (*) before the name of the variable. For example: int *ptr;

4. What is the difference between passing by value and passing by reference?

Passing by value means that the value of a variable is copied and passed to a function, while passing by reference means that the memory address of the variable is passed. This allows for the original variable to be modified within the function.

5. Are there any potential risks when using pointers?

Yes, using pointers can lead to memory leaks, where allocated memory is not properly released, causing the program to use more memory than intended. It can also result in segmentation faults, where the program tries to access memory that it does not have permission to access.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
944
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
669
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top