getting subscripted value is neither array nor pointer error

This is a discussion on getting subscripted value is neither array nor pointer error within the C Programming forums, part of the General Programming Boards category; Code: int get_circuit(void) { char buffer[100] = {0}; /* initialized to zeroes */ int i,a, rc; float x; char member0[10] ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    16

    getting subscripted value is neither array nor pointer error

    Code:
    int get_circuit(void)
    {
       char buffer[100] = {0};  /* initialized to zeroes */
       int i,a, rc;
    	float x;	
    
    	char member0[10] = "test";
    
       FILE *fp = fopen(FILE_NAME_IN,"r");
       if (fp == NULL) {
         perror("Failed to open file \"myfile\"");
       return EXIT_FAILURE;
       }
       for (i = 0; (rc = getc(fp)) != EOF && i < 	100; buffer[i++] = rc)
         ;
       fclose(fp);
    
    	//print out the contents of buffer
    	for(i=0;i<10;i++)
    	{
    		printf("buffer[%i] = %c\n",i,buffer[i]);
    
    		if(buffer[i]==' ');
    		{
    			x = atoi(buffer);
    			a++;
    		}		
    		printf("x = %f",x);
    	}
    
    	printf("ready to print the array");
    	getchar();
    	print_c_array(buffer);
    
    	strcpy(x[0].name,member0);        //<- this is the line it refers to
    
    
      	return EXIT_SUCCESS;
    }
    The structure defined as a globlal

    Code:
    struct components
    {
    char name[20];
    float value;
    }x[10];

    Thanks in advance, Mick

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    x refers to float x in this context not to your x[10] components array. Rename that float variable to something else in that function.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

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