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

  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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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);
        }
    }

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    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
    4,183
    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;
    }
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

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, 08:11 PM
  4. syntax error for another program.
    By nesagsar in forum C++ Programming
    Replies: 19
    Last Post: 12-14-2006, 04: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