Passing pointers and arrays to a C function

Click For Summary

Homework Help Overview

The discussion revolves around a C programming assignment that requires the implementation of a function to convert the case of characters in a string based on the case of the first character. The original poster expresses confusion about passing pointers and arrays to functions, particularly regarding the syntax and functionality of the `void shift(char *)` declaration.

Discussion Character

  • Exploratory, Conceptual clarification, Problem interpretation, Assumption checking

Approaches and Questions Raised

  • Participants discuss the relationship between arrays and pointers in C, noting that an array can be treated as a pointer to its first element. There are suggestions to test the first character's case before entering a loop for conversion. Some participants question the necessity of certain variables and the safety of using pointer dereferencing in loop conditions. Others propose alternative approaches to handle string length and null termination.

Discussion Status

The conversation is ongoing, with various participants providing insights and suggestions on how to structure the function and handle potential pitfalls. There is no explicit consensus on the best approach, as differing opinions on pointer usage and function design are presented.

Contextual Notes

Participants mention the use of `gets()` for input, which raises concerns about buffer overruns and null termination in strings. The discussion also touches on the implications of using null-terminated strings in C programming.

  • #31
chroot said:
You can use the safe function strnlen (notice the N, it's strnlen, not strlen) to count the length of your string for you. You must pass it the length of the array, and it will not return a value past the end of the array, even if the array does not contain a NULL.

Interestingly enough, I couldn't find strnlen() mentioned in either of my books. How many arguments does it take? Thank you.
 
Physics news on Phys.org
  • #33
Cheers!
I hope the professor won't mind me using something that's not in the text. I'll give it a try!
 
  • #34
I have a question about incrementing, if anyone has a moment. I as playing with a function example in an old book I have. It copies one string to another when you give it the arguments of pointers to the string to be copied to and the string to be copied from.

void StringCopy(char *to, char*from)
{
while(*from)
*to++ = *from++;
*to = '\0';
}

And it made me wonder ... it appears to me that moving the characters from "from" to "to" doesn't start until the pointer is at the second element, because it starts at to++ which is "to + 1" instead of starting at "to + 0". Yet it copies the first element over.
Also, it appears that the function is filling the string with null values after each completed copy to the new string. Why is this being done?
Thanks.
 
  • #35
Math Is Hard said:
And it made me wonder ... it appears to me that moving the characters from "from" to "to" doesn't start until the pointer is at the second element, because it starts at to++ which is "to + 1" instead of starting at "to + 0". Yet it copies the first element over.
Also, it appears that the function is filling the string with null values after each completed copy to the new string. Why is this being done?
Thanks.
I rewrote the code with some formatting.
Code:
void StringCopy(char *to, char*from)
{
  while(*from)
    *to++ = *from++;
  *to = '\0';
}
As you can see, the last line in the function is executed only after the loop terminates. Also note that

*to++ = *from++;

translates to

*to = *from;
to++; from++;

Hope that helps.
 
  • #36
Math is Hard,

The ++ operator when used as *from++, is a post-increment operator. The value is incremented after it is evaluated in the expression. You are confusing it with the pre-increment use, *(++from).

- Warren
 
  • #37
Thank you both. That cleared it up!

-Jessica
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 24 ·
Replies
24
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K