PM 2Ring wrote:Fancy,
Your code has several pointer-related problems. I'll give you a few hints that will hopefully help you to repair it.
• Your main() function does weird things with ptr, but I guess that's a side-effect of your find_nth() taking a pointer to a pointer.
If you need more hints, please ask.
Fancy wrote:I changed what I understood from your post, but am still a bit confused. What exactly is main doing with pointers that is weird?
I guess I should've been a bit more explicit. When I told you to "Change find_nth() to take a simple char pointer" I expected you to also change the call to find_nth() in main() to pass a plain pointer to char, not a double pointer. From the other thread, I assume you understand that in C the name of an array is a (constant) pointer to the first element of the array, so given
char str[101];f(str) passes the address of the first char in str to f().
char *ptr = str;creates space on the stack for a pointer, and stores in it the address of the first char in str.
To illustrate, let's say str[101] occupies bytes 1000 to 1100 in RAM and ptr occupies bytes 996 to 999 (it's a 32 bit system, I'm using decimal, and just making address numbers up).
So after you say
char *ptr = str;then bytes 996 to 999 ( considered as a 32 bit number) contain the value 1000.
And if you say f(ptr) or f(str), then f receives 1000 as its argument.
But if you say f(&ptr), then f receives
the address of ptr itself, which is 996. A small but highly significant difference!
As others have said, your compiler should warn you if you try to call a function with the wrong type of pointer. It's a Good Idea to set your compiler's warning level up high; eg, on gcc that's -Wall . There are even more extreme settings, but -Wall is generally adequate, IMHO, (but if someone wants to disagree, please feel free).
Fancy wrote:Also, the exact programming problem (followed by my updated attempt at coding it.)
Design and test a function that fetches the next n characters from input (including blanks, tabs, and newlines), storing the results in an array whose address is passed an argument.
Come to think of it, I think I might be going at this all wrong. Maybe a getchar() for count chars and then into an array would work better?
Yes, using getchar() is probably a good idea. fgets() is NOT really adequate for this task: it's for reading lines of text, so it stops reading chars when it sees a newline (or end of file). So you want a loop that reads chars using getchar() which exits when it gets the desired
count number of chars or if EOF is reached. And don't forget to make sure that
count isn't too big for your array.
Fancy wrote:Code: Select all
#include <stdio.h>
void find_nth(char *str, int n);
int main(void) {
int n;
char str[101];
char *ptr = str;
puts("Enter a string:");
fgets(str, 100, stdin);
puts("How many characters would you like printed?");
scanf("%d", &n);
find_nth(&ptr, n);
printf("Your string up to the %d char is %s\n", n, str);
return 0;
}
void find_nth(char *str, int n) {
int count;
for(count = 0; count < n; count++)
; /*Intentional null statement, NOT an accident*/
*(str + count + 1) = '\0';
}
FWIW, the loop
Code: Select all
for(count = 0; count < n; count++)
; /*Intentional null statement, NOT an accident*/
Is a bit silly, it accomplishes the same thing as
count = n;Anyway, it's getting rather late here, so I'd better go and get some sleeep.
