Cant find the syntax error....and clarification on what my program is doing.

This is a discussion on Cant find the syntax error....and clarification on what my program is doing. within the C Programming forums, part of the General Programming Boards category; gcc says: In function 'main': error: syntax error before "int" error: syntax error before "char" Code: #include <stdio.h> int main(){ ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    18

    Cant find the syntax error....and clarification on what my program is doing.

    gcc says: In function 'main':
    error: syntax error before "int"
    error: syntax error before "char"

    Code:
    #include <stdio.h>
    
    int main(){
        int i;
        
        char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
        int int_array[5] = {1,2,3,4,5};
        
        char *char_pointer;
        int *int_pointer;
        
        char_pointer = (char*) int_array; /*typecast into the pointers data type*/
        int_pointer = (int*) char_array;
        
        for(i=0; i < 5; i++){
            printf("[integer pointer] points to %p, which is contains the
            char '%c'\n", int_pointer, *int_pointer);
        int_pointer = (*int) ((char*)int_pointer + 1);
        }
        
        for(i=0; i < 5; i++){
            printf("[char pointer] points to %p, which contains the int %d\n",
            char_pointer, *char_pointer);
        char_pointer = (*char) ((int*)char_pointer + 1);
        }
    }
    i am following a book and the books doesnt really explain: char_pointer = (*char) ((int*)char_pointer + 1);
    i would like to know what that piece of code is doing. also because ive already typecast in the beginning. so i dont see why its repeated.

    EDIT: I also dont understand the argument. isnt typecasting supposed to have this format: (typecast_data) variable ? so wouldnt it be just ((int*) char_pointer+1); ?
    Last edited by prafiate; 06-11-2012 at 04:12 PM.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Change those to these..
    Quote Originally Posted by prafiate View Post
    Code:
    #include <stdio.h>
    
    int main(){
        int i;
        
        char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
        int int_array[5] = {1,2,3,4,5};
        
        char *char_pointer;
        int *int_pointer;
        
        char_pointer = (char*) int_array; /*typecast into the pointers data type*/
        int_pointer = (int*) char_array;
        
        for(i=0; i < 5; i++){
            printf("[integer pointer] points to %p, which is contains the
            char '%c'\n", int_pointer, *int_pointer);
        int_pointer = (int*) ((char*)int_pointer + 1);
        }
        
        for(i=0; i < 5; i++){
            printf("[char pointer] points to %p, which contains the int %d\n",
            char_pointer, *char_pointer);
        char_pointer = (char*) ((int*)char_pointer + 1);
        }
    }
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    66
    Can you list the line number for the errors ?
    let me guess that they are 18 and 24

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    Fixed the errors and warnings.
    NOTE: *int is NOT correct.

    Tim S.

    Code:
    #include <stdio.h>
    
    int main(){
        int i;
    
        char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
        int int_array[5] = {1,2,3,4,5};
    
        char *char_pointer;
        int *int_pointer;
    
        char_pointer = (char*) int_array; /*typecast into the pointers data type*/
        int_pointer = (int*) char_array;
    
        for(i=0; i < 5; i++){
            printf("[integer pointer] points to %p, which is contains the char '%c'\n", int_pointer, *int_pointer);
            int_pointer = (int*) ((char*)int_pointer + 1);
        }
    
        for(i=0; i < 5; i++){
            printf("[char pointer] points to %p, which contains the int %d\n",
            char_pointer, *char_pointer);
            char_pointer = (char*) ((int*)char_pointer + 1);
        }
    
        return 0;
    }
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    Quote Originally Posted by prafiate View Post
    EDIT: I also dont understand the argument. isnt typecasting supposed to have this format: (typecast_data) variable ? so wouldnt it be just ((int*) char_pointer+1); ?
    It appears to be doing a very poor job of teaching pointer math.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I cannot find out the error in the program.
    By from_china in forum C Programming
    Replies: 9
    Last Post: 06-19-2011, 12:11 AM
  2. Clarification for a complicated syntax
    By manasij7479 in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2011, 12:37 PM
  3. can't find error(s) in my program..help?
    By jennyt in forum C Programming
    Replies: 1
    Last Post: 11-23-2010, 07:11 PM
  4. syntax error for another program.
    By nesagsar in forum C++ Programming
    Replies: 19
    Last Post: 12-14-2006, 03:07 PM
  5. Need Help to Find the Error in this program
    By sangken in forum C Programming
    Replies: 7
    Last Post: 10-03-2006, 06:12 PM

Tags for this Thread


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