Beginner- fgetc, arrays, and singly linked lists

This is a discussion on Beginner- fgetc, arrays, and singly linked lists within the C Programming forums, part of the General Programming Boards category; Hi everyone, I have a couple of beginner questions concerning the fgetc function, arrays, and singly linked lists. To make ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    2

    Beginner- fgetc, arrays, and singly linked lists

    Hi everyone, I have a couple of beginner questions concerning the fgetc function, arrays, and singly linked lists. To make it easier, I'll break everything into three separate questions.

    Question 1

    This is the code taken from the tutorial about command line arguments

    Code:
    #include <stdio.h>
    
    int main ( int argc, char *argv[] )
    {
        if ( argc != 2 ) /* argc should be 2 for correct execution */
        {
            /* We print argv[0] assuming it is the program name */
            printf( "usage: %s filename", argv[0] );
        }
        else 
        {
            // We assume argv[1] is a filename to open
            FILE *file = fopen( argv[1], "r" );
    
            /* fopen returns 0, the NULL pointer, on failure */
            if ( file == 0 )
            {
                printf( "Could not open file\n" );
            }
            else 
            {
                int x;
                /* read one character at a time from file, stopping at EOF, which
                   indicates the end of the file.  Note that the idiom of "assign
                   to a variable, check the value" used below works because
                   the assignment statement evaluates to the value assigned. */
                while  ( ( x = fgetc( file ) ) != EOF )
                {
                    printf( "%c", x );
                }
            }
            fclose( file );
        }
    }
    I was wondering if the fgetc function gets executed in the while loop.

    Question 2

    This code was also taken from the C tutorial.

    Code:
    FILE *fp;
    fp=fopen("c:\\test.bin", "wb");
    char x[10]="ABCDEFGHIJ";
    fwrite(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), fp);
    What exactly does char x[10]="ABCDEFGHIJ" do to the array? I know that we have an array of characters with 10 elements but is the first element A, second element B etc...?


    Question 3

    Last question

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node {
      int x;
      struct node *next;
    };
    
    int main()
    {
        /* This won't change, or we would lose the list in memory */
        struct node *root;       
        /* This will point to each node as it traverses the list */
        struct node *conductor;  
    
        root = malloc( sizeof(struct node) );  
        root->next = 0;   
        root->x = 12;
        conductor = root; 
        if ( conductor != 0 ) {
            while ( conductor->next != 0)
            {
                conductor = conductor->next;
            }
        }
        /* Creates a node at the end of the list */
        conductor->next = malloc( sizeof(struct node) );  
    
        conductor = conductor->next; 
    
        if ( conductor == 0 )
        {
            printf( "Out of memory" );
            return 0;
        }
        /* initialize the new memory */
        conductor->next = 0;         
        conductor->x = 42;
    
        return 0;
    }
    Since conductor is pointing to the node root is pointing to and we set the node root is pointing to to the null pointer , wouldn't the while loop be effectively skipped and a new node created?

    Thanks for the help!

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    1- Yup, it does.
    2-that's right, it gets initialized with that string.
    3-yes, i think so

    btw if you just started to learn C i think you're going way too fast
    Last edited by thefeedinghand; 07-14-2010 at 09:43 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. Yes. Every pass through the loop it calls fgetc, and assigns its value to x, then tests it for EOF.
    2. Make an array of 10 characters, and assign those ten characters to the array elements. You could also do:
    Code:
    char foo[10] = { 'A', 'B', 'C', 'D', /*and all the rest */ };
    3. The loop that goes through each ->next, trying to find the end, doesn't execute because the first check is false. The loop body is skipped, the check still goes through.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    probably worth pointing out that the null character '\0' also gets inserted into the array right after the string which marks, of course, the end of string
    Last edited by thefeedinghand; 07-14-2010 at 09:57 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by thefeedinghand View Post
    probably worth pointing out that the null character '\0' also gets inserted into the array right after the string which marks, of course, the end of string
    Not in this case it doesn't. ABCDEFGHIJ is 10 characters, so there is no room for the nul character. This isn't a string in this case, it's just an array of 10 characters.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    2
    Quote Originally Posted by thefeedinghand View Post
    probably worth pointing out that the null character '\0' also gets inserted into the array right after the string which marks, of course, the end of string
    Hey, thanks for the help. Do you have any suggestions if I am just starting out? I'm going through the C tutorial one lesson at a time and I will probably get a book to go over during the summer.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21