Thread: late night coding

  1. #16
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    okay I'm slowly getting it.
    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;   
    }
    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.
    My mistake was assuming that
    Code:
    return vault
    automatically 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.
    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.
    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;
        }
    }
    This is the full code in my book in his call to main notice he takes some exception handling into mind.
    Code:
      while (getword(input) != NULL)
            puts(input);
    so even though it returns "" he ignores that and prints from the biggining of the string. Thats why your code
    Code:
    char buffer[BUFSIZ];
    printf ( "%s\n", getword ( buffer ) );
    printf ( "%s\n", buffer );
    printed out the "" and then the word. what the getword(buffer) returned and what was actally in bufffer. EUREKA!!!!
    i'm getting it. and thinking out loud. So my other fragments worked
    Code:
    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;
    }
    and
    Code:
    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;
    }
    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 like
    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;
    }
    if this is correct. Was my mistake then thinking that vault was a character array and returning vault would automatically go to vault[0].


    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.
    I was about to dispute this and then i did this
    Code:
    /* 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; &copy = %p; value = %p\n",
           copy+1, &copy, copy);
      getchar();
    
      return 0;
    }
    output
    Code:
    Don't be a fool!
    mesg = Don't be a fool!; &mesg = 0022FF6C; value = 00401280
    copy = on't be a fool!; &copy = 0022FF68; value = 00401280
    same address, different ouput. That what your getting at? I never knew the compiler or C could do that. I assumed....wrong.

    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 was making a fatal mistake. ptrs !=arrays. I think that was it. Please tell me lesson learned.

    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.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Since arrays use ptr notation underneath.
    This is the part where you start hating me. Arrays do not use pointer notation, pointers use array notation. Except for three very specific situations, an array name is converted to a pointer to the first element. So array subscripting is really a convenient sugar coating on top of pointer arithmetic.

    >The word was caputred I was just pointing to the wrong address which was NULL.
    The address you were pointing to contained the value '\0', yes. It's a good idea to be very specific when you use NULL because NULL is a macro designating a null pointer, which is different from the null character '\0'.

    >So my other fragments worked
    Yes, but not like you expected. You were treating vault as an array when it was really a pointer. Remember that
    Code:
    void f ( char *a );
    and
    Code:
    void f ( char a[] );
    Mean the exact same thing, both designate a as a pointer to char. Unlike a "real" array, you can reseat a and make it point somewhere else. I think that is where your confusion lies. When you changed vault, you didn't realize that you were reseating the pointer and that the reseated pointer would be treated as the beginning of the array instead of the beginning you expected (this is poorly worded, I'm sorry).

    >Was my mistake then thinking that vault was a character array and returning vault would automatically go to vault[0].
    Yes, precisely.

    >That what your getting at?
    Yep. A pointer is just another variable that holds a value.

    >I was making a fatal mistake. ptrs !=arrays.
    And another sees the light. Pointers and arrays are just similar enough to cause all but the most experienced C programmers fits.

    >I have to look up more into NULL and exactly what it does
    It's a pointer value that lets you say "no pointer here". The actual value of NULL is something along the lines of ( (void *)0 ), and you can use the integral constant 0 interchangeably with NULL. Just remember that there's nothing magical about a null pointer, it's just a way to give a pointer a value without actually pointing it to anything, thus designating an invalid pointer that can be tested. Note that this is different from an uninitialized pointer that has no predictable value.

    >I assume he will go into more details in later chapters
    What book?
    My best code is written with the delete key.

  3. #18
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Arrays do not use pointer notation, pointers use array notation. Except for three very specific situations, an array name is converted to a pointer to the first element. So array subscripting is really a convenient sugar coating on top of pointer arithmetic.
    That actually makes sense and I get that. Don't hate you yet. Maybe when i find out what those three specific situations are.

    Pointers and arrays are just similar enough to cause all but the most experienced C programmers fits.
    Once again, i thought I had covered and understood all I needed to know about ptrs and then something shows up again. Well practice, practice, practice. That's why the board is here.

    What book?
    This book he does things a certain way but I suspect I will see different POV's when I read other books and tutorials.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Maybe when i find out what those three specific situations are.
    Sizeof, address of, and string literal array initialization. These are work with arrays as objects, not as values, so the conversion to a pointer is not made:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char a[] = "This is a test"; /* Object context */
    
      printf ( "%p\n", (void *)&a ); /* Object context */
      printf ( "%lu\n", (unsigned long)sizeof a ); /* Object context */
    
      return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectShow code - long night of coding
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 03-27-2006, 10:50 AM
  2. Late night part 2
    By caroundw5h in forum C Programming
    Replies: 5
    Last Post: 09-09-2004, 10:07 AM
  3. Good times, late at night
    By prog-bman in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-15-2004, 09:02 AM
  4. late night coding
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 01-08-2004, 09:23 PM
  5. friday night coding with mind numbing music
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-13-2002, 05:17 PM