Thread: multiple errors on one line - C

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

    Question multiple errors on one line - C

    Hey guys... i'm new to the programming scene, i'm totally lost as to how to fix this problem.

    Function:
    Code:
    typedef struct{
    			char id;
    			double x;
    			double y;
    			double power;
    			double radius;
    			}towers_t;
    
    void
    stage_2(towers_t stage_1){
    int i=0;
    while(i<MAXTOWERS){
    	if(i==EOF){
    	}
    	else{
    		towers[i].radius = 10.0*pow((towers[i].power/PMIN),.25)-10.0);
    		i++;
    	}
    
    	printf("Tower %c at (%4lf,%4lf) transmitting %.2lf W has a signal radius %4lf\n", towers.id, towers.x, towers.y, towers.power, towers.radius);
    }
    }
    The error is on the line with the calculation.
    The problems are stated as :
    • syntax error
    • subscripted value is neither array nor pointer
    • syntax error before ')' token
    Last edited by djdato; 05-15-2010 at 09:02 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > towers.radius[i] =
    None of what you've posted is an array (that's the "subscripted value is neither array nor pointer")

    towers isn't an array.
    Each member of the struct isn't an array either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    11
    then how would i pass values into a single structure member already existing in an array of structures?
    To clarify:
    In my first function i have already put values into a struct. In my 2nd function i want to add to that same struct. That struct is part of an array with other structs of the same type, and i want to be able to enter the values for towers.radius with this single calculation.

    Hope that clears up what im trying to say.
    Last edited by djdato; 05-15-2010 at 08:02 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Show us how you're calling stage2

    Are you passing just one element of the array from the caller?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    11
    i'm not calling stage2. im passing stage1 into stage2.

    stage 1 has the user input data for id, x, y and power and it puts that into the 1st structure of an array. If the user puts in a new line of data it puts that into a 2nd structure of the array, and so on.

    in stage 2 i'm passing in stage1. i just want to add an extra member of information via the calculation then print out a table.

    *edit* sorry i put the wrong input for stage_2 (fixed it)
    Last edited by djdato; 05-15-2010 at 09:02 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Not enough information.

    Is stage1 an array of towers, or just one?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    11
    stage1 is an array of many towers.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    11
    i have found another of ur posts taht i maybe could use to show wat i mean.
    you posted this in another recent post:

    Code:
    struct pet menagerie[3];  // all my pets
    
    menagerie[0].species = "Labrador";
    menagerie[0].name = "Scooby";
    menagerie[0].numLegs = 4;
    
    menagerie[1].species = "Parrot";
    menagerie[1].name = "Polly";
    menagerie[1].numLegs = 2;
    
    menagerie[2].species = "Goldfish";
    menagerie[2].name = "Jaws";
    menagerie[2].numLegs = 0;
    basically i have all my species' and name's filled in via stage1 but not the numLegs'. I now want to compute the data for numLegs' in stage2.
    ...perhaps the way i'm approaching it is not proper? @~@
    Last edited by djdato; 05-15-2010 at 09:25 AM.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    11
    Ok i did something... i don't really remember what i did...i was jsut playing around with it but i've managed to get it down to one error.

    Code:
    void
    stage_2(towers_t stage_1){
    int i=0;
    while(i<MAXTOWERS){
    	if(i==EOF){
    	}
    	else{
    		towers[i].radius = (10.0*pow((towers[i].power/PMIN),.25)-10.0);
    		i++;
    	}
    
    	printf("Tower %c at (%4lf,%4lf) transmitting %.2lf W has a signal radius %4lf\n",
    			towers.id, towers.x, towers.y, towers.power, towers.radius);
    error: `towers' undeclared (first use in this function)

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by djdato View Post
    error: `towers' undeclared (first use in this function)
    Well, like it says, "towers" is undeclared.
    Code:
    void
    stage_2(towers_t stage_1){
    int i=0;
    while(i<MAXTOWERS){
    	if(i==EOF){
    	}
    	else{
    		towers[i].radius = (10.0*pow((towers[i].power/PMIN),.25)-10.0);
    		i++;
            }
    The declared variables in stage_2() are red. I don't see any named "towers".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    11
    im not sure if what i'm doing is possible...
    but i'm trying to pass in stage_1 which returns an array called 'towers'.
    Is there a way to modify an array which is outside the function? Or can I pass that array into the function?
    Last edited by djdato; 05-15-2010 at 10:20 AM.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    pass the arrays address and size into the function

    ex.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void func(int arr_size, int* arr)
    {
    //this function initializes the arr array that it receives from main
    int i;
    for(i=0;i<arr_size;++i)
    	arr[i] = i+1;
    }
    
    int main()
    {
    int k;
    int *myarray = malloc(100*sizeof(int));
    func(100,myarray); //myarray is a pointer
    for(k=0;k<100;++k) printf("%d ", myarray[k]);
    
    // or with a static array
    int myarray2[100];
    func(100,&myarray2[0]);
    for(k=0;k<100;++k) printf("%d ", myarray2[k]);
    }
    since now 'func' has the actual location of the array, it can modify it and any changes made reflect back in main.
    Last edited by rodrigorules; 05-15-2010 at 12:07 PM.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    11
    thanks guys. fixed it. did what rodrigo told me to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do encryption in C
    By sankarv in forum C Programming
    Replies: 33
    Last Post: 12-28-2010, 11:01 AM
  2. Multiple command line arguments with switches
    By Ene Dene in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2009, 03:28 AM
  3. Imposing Line Numbers automatically in the C code
    By cavestine in forum C Programming
    Replies: 14
    Last Post: 10-15-2007, 12:41 AM
  4. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM

Tags for this Thread