Thread: Pointer confusion

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    24

    Pointer confusion

    Ok I'm getting slightly insane here.
    Before I begin to ask my question I will give you some code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include "misc.h"
    
    #define MAX_ANSWERS 3
    
    typedef struct quest
    {
            char question[100];
            char answer[MAX_ANSWERS][100];
            int  correct[MAX_ANSWERS];
            struct quest* next;
    } QUEST;
    
    void make_question(QUEST** q);
    void make_quiz(QUEST** phead);
    
    int main(void)
    {
        QUEST* head = NULL;
        
        make_quiz(head);
        getchar();
        return 0;
    }
    
    void make_quiz(QUEST** phead)
    {
         QUEST* q = NULL, *qprev = NULL;
         char newquest = 'j';
         int i=0;
         
         phead = malloc(sizeof(QUEST));
         
         while(newquest == 'j')
         {
             printf("*** Vraag %d\n-\n\n", ++i);
             make_question(q);
             printf("-> %s", q->question);
             if (qprev!=NULL)
                qprev->next = q;
             else
                phead = q;
             qprev = malloc(sizeof(QUEST));
             qprev = q;
             printf("Wil je nog een vraag toevoegen ? (j/n): ");
             scanf("%c", &newquest);
             fflush(stdin);
         }
         
    }
    
    void make_question(QUEST** pq)
    {
         int i, correctgiven=0;
         char correct;
              
         pq = malloc(sizeof(QUEST));
         printf("Typ je vraag: ");
         scanf("%[^\n]%*c", &pq->question);
         fflush(stdin);
         
         for(i=1;i<=MAX_ANSWERS;i++)
         {
               printf("\nAntwoord %d: ", i);
               scanf("%[^\n]%*c", &pq->answer[i]);
               fflush(stdin);
               
               if (!correctgiven)
               {
                  printf("Is dit antwoord correct ? (j/n)");
                  scanf("%c", &correct);
                  fflush(stdin);
                  
                  if (correct == 'j')
                  {
                     correctgiven = 1; 
                     pq->correct[i] = 1;
                  } else
                     pq->correct[i] = 0;
               }
         }
         &pq->next = NULL;
    }
    I want to build a linked list of QUEST's.
    First I call make_quiz() wich calls make_question() in a loop.
    I give "head" along with make_quiz (byval) wich has to assign an address to head. But it just doesn't work.

    What am I doing wrong ?

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    >make_quiz(head);
    &head
    >fflush(stdin);
    check the faq on why your compiler may not support this.
    Last edited by qqqqxxxx; 03-24-2006 at 05:43 AM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    Ok, I changed head to &head and also q into &q.
    These are the errors I get:

    Code:
    makequiz2.c: In function `make_quiz':
    makequiz2.c:46: warning: assignment from incompatible pointer type
    
    makequiz2.c: In function `make_question':
    makequiz2.c:63: error: request for member `question' in something not a structure or union
    makequiz2.c:69: error: request for member `answer' in something not a structure or union
    makequiz2.c:81: error: request for member `correct' in something not a structure or union
    makequiz2.c:83: error: request for member `correct' in something not a structure or union
    makequiz2.c:86: error: request for member `next' in something not a structure or union
    
    make.exe: *** [makequiz2.o] Error 1
    Last edited by ReggieBE; 03-24-2006 at 05:45 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you need to pass a pointer to the QUEST pointer, as shown below.
    Code:
        QUEST* head = NULL;
        
        make_quiz(&head);
    Then make_quiz need to dereference that pointer so that it can allocate memory to main()'s pointer.
    Code:
    void make_quiz(QUEST** phead)
    {
        ...     
         *phead = malloc(sizeof(QUEST));
    
      ...
        (*pq)->next = NULL;
    and do similar stuff in other functions

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    YES ! That did it thnx !

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    Ok I have another problem now. I tried to save the struct one by one. And this is what I get:

    -----------------------
    DIt is vraag 1 \QTSystem\;C:\Program Files\Java\jdk1.5.0_03\bin; PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WS F;.WSH PROCESSOR_ARCHITECTURE=x86 PROCESSOR_IDENTIFIER=x86 Family 6 Model 6 Stepping ja AuthenticAMD PROCESSOR_LEVEL=6 PROCESSOR_REVISION=0602 ProgramFiles=C:\Program Files QTJAVA=C:\Prnee m Files\Java\jre1.5.0_05\lib\ext\QTJava.zip SESSIONNAME=Console SystemDrive=C: SystemRoot=C:\WINmisschien C:øK= Dit is nog een vraag nee ja misschien
    ------------------------

    Is this normal ? lol
    Here's the code:

    Code:
    void save_quiz(QUEST* phead, char category[20])
    {
         FILE* fp = NULL;
         QUEST* q = NULL;
         char filename[50] = {'\0'};
         int i=0;
         
         getfilename("rubrieken/", category, filename);
         fp = fopen(filename, "w");
         
         q = phead;
         while(q!=NULL)
         {
             fwrite(q, sizeof(QUEST), 1, fp);
             q = q->next;
         }
         fclose(fp);
         
         printf("Quiz saved !");
    }
    Last edited by ReggieBE; 03-24-2006 at 06:04 AM.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    your program is writing the structures in binary format, so yes you will probably see a lot of trash in the file if you look at it with a normal text editor such as Notepad.exe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer confusion
    By rohit_second in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 04:25 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Pointer confusion...
    By fkheng in forum C Programming
    Replies: 13
    Last Post: 06-23-2003, 10:18 PM