Passing pointers and arrays to a C function

Click For Summary
SUMMARY

The discussion centers on implementing a C function, void shift(char *), which modifies a string based on the case of its first character. Participants clarify that passing a pointer to the first character of an array is sufficient for the function to operate on the entire string. The conversation emphasizes the importance of null-termination in strings and suggests improvements for efficiency, such as combining case checks within a single loop. Additionally, there is a consensus on the necessity of validating input to prevent potential crashes due to malformed strings.

PREREQUISITES
  • Understanding of C programming language syntax and semantics
  • Familiarity with pointers and arrays in C
  • Knowledge of string manipulation functions in C, specifically isupper() and tolower()
  • Awareness of memory management concepts in C, including dynamic and static allocation
NEXT STEPS
  • Implement error handling for null pointers in C functions
  • Explore the differences between gets() and fgets() for safer string input
  • Learn about the implications of null-terminated strings in C programming
  • Investigate performance optimization techniques in C, particularly regarding loop efficiency
USEFUL FOR

C programmers, software developers working with string manipulation, and anyone interested in improving their understanding of pointers and memory management in C.

  • #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 17 ·
Replies
17
Views
2K
  • · Replies 23 ·
Replies
23
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