Thread: syntax error before '?' token

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    81

    syntax error before '?' token

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Employee{
           int age;
           int a;
    };
    
    int main(void){
        int i, rows;
        struct Employee *Array;
      
               printf("Please type how big the array you want to be:\n");
               scanf("%d", &rows);
               
               Array = (struct Employee *)malloc(rows*sizeof(struct Employee));
      
               /* Testing if everything went well */
               
               for(i=0; i<rows; i++){
                        Array[i] = ?;
                        printf("A[%d] = %d\n", i, Array[i]);
               }
               
      system("PAUSE");
      return 0;
    }
    Insteed of ? what can i put so as to compile right and run?

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Data.

    EDIT: ::yawns:: okay, I guess I'll expand this a bit. What do you think an array is? How do you "print" a struct? Is this array a %d in the printf? The operator ? is called a tertiary and is used like (<comparison> ? <response if true> : <response if false>).
    Last edited by Kennedy; 05-25-2010 at 08:44 PM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    81
    as? numbers? characters? can you give me an example?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I've got this program...

    Code:
    int main()
    {
        ?;
    }
    What should I put in there?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    81
    whatever you want....some commands...there is no "special" answer...right?

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What makes you think that you can print a struct using %d?

    A better question: Even if that worked, what would you expect the program to output?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
        for(i=0; i<rows; i++){
                      struct Employee dummy = { 0 , 0 };
                        Array[i] = dummy;             /* struct assignment */                    
               }
    Code:
         // using C99
                     for(i=0; i<rows; i++){
                        Array[i] = (struct Employee) { .age = 0, a. = 0 };  
                     }

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
         // using C99
                     for(i=0; i<rows; i++){
                        Array[i] = (struct Employee) { .age = 0, a. = 0 };  
                     }
    I'm still bitter that they left that facility out of the C++ standard. Arghh!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    I think maybe you haven't got a good handle on how structures work. What youve set up is an array of employees, each with an age and an "a", whatever that is. You'd access the age of employee number 15 as Array[15].age. Look at the modification I made to your code and see if you can follow how it works.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Employee{
           int age;
           int a;
    };
    
    int main(void){
        int i, rows;
        struct Employee *Array;
      
               printf("Please type how big the array you want to be:\n");
               scanf("%d", &rows);
               
               Array = (struct Employee *)malloc(rows*sizeof(struct Employee));
      
               /* Testing if everything went well */
               
               for(i=0; i<rows; i++){
                        Array[i].age = i;
                        Array[i].a   = 100+i;
                        printf("A[%d] age = %d\n", i, Array[i].age);
                        printf("A[%d] a   = %d\n", i, Array[i].a);
               }
               
      system("PAUSE");
      return 0;
    }

    H.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    81
    Thank you very much "THEBigH". This was what i was looking for! I understood your example, i just don' t know structs yet...i am on a learning phase...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM