okay I'm slowly getting it.ch was saved in vault and it does increment, but as you said when I returned vault it was pointing to noting cause that was what I assigned '\0' to last.Code:char *GetWord(char *vault){
//get word using advance pointer notation
int ch;
while( (ch = getchar()) !=STOP && ch!='\n' && ch !=EOF)
*vault++ = ch;
*vault ='\0';//whatever indice vault is at
while ( (getchar()) !='\n');
return vault;
}
My mistake was assuming thatautomatically points to the biggining of vault. Perhaps i was assuming it was done the same way as an array. Since arrays use ptr notation underneath.Code:return vault
when you return an array you return array[0]. And this would return array[0] and start printing from there.
this is where your saying i'm wrong correct. The word was caputred I was just pointing to the wrong address which was NULL.
This is the full code in my book in his call to main notice he takes some exception handling into mind.Code:/* Programming Exercise 11-3 */
#include <stdio.h>
#define LEN 80
char * getword(char * str);
int main(void)
{
char input[LEN];
char *chk;
while (getword(input) != NULL)
puts(input);
puts("Done.\n");
getchar();
return 0;
}
#include <ctype.h>
char * getword(char * str)
{
//int i;
int ch;
while ((ch = getchar()) != EOF && !isspace(ch))
*str++ = ch;
*str = '\0';
if (ch == EOF)
return NULL;
else
{
while (ch != '\n')
ch = getchar();
return str;
}
}
so even though it returns "" he ignores that and prints from the biggining of the string. Thats why your codeCode:while (getword(input) != NULL)
puts(input);
printed out the "" and then the word. what the getword(buffer) returned and what was actally in bufffer. EUREKA!!!!Code:char buffer[BUFSIZ];
printf ( "%s\n", getword ( buffer ) );
printf ( "%s\n", buffer );
i'm getting it. and thinking out loud. So my other fragments workedandCode:char *GetWord(char vault[]){
int ch,ndx=0;
while( (ch = getchar()) !=STOP && ch !='\n' && ch !=EOF){
vault[ndx] = ch;
ndx+=1;
}
vault[ndx] = '\0';
while((getchar()) !='\n');
return vault;
}
because i had used it as a char array and on returning i returned array[0]. These fragments kept count of where i was in the array at any given time. is this correct. please say yes?? kinda likeCode:
char *GetWord(char vault[]){
//get word using pointer notation
int ch,ndx=0;
while( (ch = getchar ()) != STOP && ch !='\n' && ch !=EOF){
*(vault + ndx) = ch;
ndx+=1;
}
*(vault + ndx) = '\0';
while((getchar() ) !='\n');
return vault;
}
if this is correct. Was my mistake then thinking that vault was a character array and returning vault would automatically go to vault[0].Code:#include <ctype.h>
#include <stdio.h>
char *getword ( char *buffer )
{
int i = 0;
int ch;
while ( ( ch = getchar() ) != EOF && !isspace ( ch ) ) {
*buffer++ = (char)ch;
++i;
}
*buffer = '\0';
return buffer - i;
}
int main ( void )
{
char buffer[BUFSIZ];
printf ( "%s\n", getword ( buffer ) );
return 0;
}
I was about to dispute this and then i did thisQuote:
when you increment *p++ isn't that incrementing buffer as well?
No, they are two different pointers that just happen to point to the same address. When you increment p, buffer still points to the same location that it did before you incremented p. p and buffer are different objects even though the address they point to is the same.
outputCode:/* p_and_s.c -- pointers and strings */
#include <stdio.h>
int main(void)
{
char * mesg = "Don't be a fool!";
char * copy;
copy = mesg;
printf("%s\n", copy);
printf("mesg = %s; &mesg = %p; value = %p\n",
mesg, &mesg, mesg);
printf("copy = %s; © = %p; value = %p\n",
copy+1, ©, copy);
getchar();
return 0;
}
same address, different ouput. That what your getting at? I never knew the compiler or C could do that. I assumed....wrong.Code:Don't be a fool!
mesg = Don't be a fool!; &mesg = 0022FF6C; value = 00401280
copy = on't be a fool!; © = 0022FF68; value = 00401280
I was making a fatal mistake. ptrs !=arrays. I think that was it. Please tell me lesson learned. :oQuote:
it always goes to the biginning of that block of memory right?
How do you define the beginning of a block? It's solely dependent on the logic of your program, so pointers cannot magically move to the beginning of a block unless you explicitly tell them to.
I have to look up more into NULL and exactly what it does because my book doesnt' go into to much detail except to say that it is a address called the null pointer. Sort of like the string version of EOF. I assume he will go into more details in later chapters.