Odd Error message

This is a discussion on Odd Error message within the C Programming forums, part of the General Programming Boards category; Hello, I am pretty proficient with C++. However, I am new to plain C programming. I am attempting to compile ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Odd Error message

    Hello,

    I am pretty proficient with C++. However, I am new to plain C programming.

    I am attempting to compile the following code on a UNIX system. For some reason I get error messages indicated in the first code block. The second code block is my code. Can someone point out what I am doing wrong? The error messages don't seem to make sense.

    Code:
    "secImg.c", line 20: syntax error before or at: char
    "secImg.c", line 23: undefined symbol: i
    "secImg.c", line 23: undefined symbol: buf
    Code:
    #include <stdio.h>		//for printf
    #include <fcntl.h>		//for file open operations
    #define FILE_NAME_SIZE 256	//for the size of a character
    
    main(int args, char * argv[])
    {
    	char * nameBuff = (char *)malloc(sizeof(char) * 100);
    	
    	if(nameBuff != NULL)
    		strcpy(nameBuff, "malloc worked\n");
    	
    	else
    		printf("malloc failed to allocate memory\n");
    	
    	printf(nameBuff);	
    	free(nameBuff);
    	
    	char * buf = (char *)malloc(FILE_NAME_SIZE);
    	int i;
    	
    	while(i = read(0, buf, FILE_NAME_SIZE))
    	{ write(1, buf, i); }
    	
    	return 0;
    
    }

  2. #2
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Im pretty sure the compiler is complaining about the declerations of the int i, and char buf, in C they should be made at the top of any set of { brace.

    Also you are using strcpy without including <string.h>, and you should also include <stdlib.h>.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>For some reason I get error messages indicated in the first code block.
    That reason would be that C is not C++, unless you use the latest and greatest C standard you'll have a hard time declaring variables anywhere except at the beginning of a block. :-)

    You should also include stdlib.h and string.h to avoid undefined behavior from your use of malloc and strcpy. Undefined behavior is yucky. ;-)
    Code:
    #include <stdio.h>//for printf
    #include <fcntl.h>//for file open operations
    #include <stdlib.h>
    #include <string.h>
    #define FILE_NAME_SIZE 256//for the size of a character
    
    main(int args, char * argv[])
    {
      int i;
      char * buf;
      char * nameBuff = (char *)malloc(sizeof(char) * 100);
      
      if(nameBuff != NULL)
        strcpy(nameBuff, "malloc worked\n");
      
      else
        printf("malloc failed to allocate memory\n");
      
      printf(nameBuff);
      free(nameBuff);
      
      buf = (char *)malloc(FILE_NAME_SIZE);
      
      while(i = read(0, buf, FILE_NAME_SIZE))
      { write(1, buf, i); }
      
      return 0;
      
    }
    *Cela*

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    A few remarks.

    1. Don't forget to free buf at the end of main().
    2. Free and print nameBuff only if malloc() worked.
    3. Also check buf, could be NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum all odd array elements?
    By Marth_01 in forum C Programming
    Replies: 9
    Last Post: 11-04-2008, 09:47 PM
  2. Odd and even
    By swgh in forum C++ Programming
    Replies: 12
    Last Post: 09-18-2006, 05:56 PM
  3. adding odd numbers only
    By CheyenneWay in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2004, 12:22 AM
  4. new type: even or odd
    By nbo10 in forum C++ Programming
    Replies: 7
    Last Post: 09-05-2003, 11:17 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:56 PM

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