Thread: Can anyone help me with these errors?

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

    Can anyone help me with these errors?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Files{
    	char name[40];
    	int years;
    	int age;
    };
    
    
    void f_age(struct Files data, int num){
    	int i;
    	
    	for(i=0; i<num; i++){
    		printf("Please type your age:\n");
    		scanf("%d", &data[i].age);
    	}	
    
    }
    
    
    int main(void){
    	int i, number;
    	struct Files *data;
    	
    	printf("Please type a number for the variable number:\n");
    	scanf("%d", &number);
    	
    	data=(struct Files *)malloc(number*sizeof(struct Files));
    	
    	f_age(data, number);
    	
    //	for(i=0; i<number; i++){
    //		printf("Please type your age:\n");
    //		scanf("%d", &data[i].age);
    //	}	
    	
    	for(i=0; i<number; i++){
    		printf("Age[%d]: %d\n", i, data[i].age);
    	}
    	
    	return 0;
    }
    output: vasileios@vasileios-desktop:~/desktop$ gcc askisi1.c

    askisi1.c: In function ‘f_age’:
    askisi1.c:17: error: subscripted value is neither array nor pointer
    askisi1.c: In function ‘main’:
    askisi1.c:32: error: incompatible type for argument 1 of ‘f_age’
    askisi1.c:12: note: expected ‘struct Files’ but argument is of type ‘struct Files *’

    I am not so good in structs...and...working with fuctions but i still try to learn...there are some errors when i call the f_age in main and also when i initialize the variables...if anyone khows what's wrong please tell me...thanks...

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    void f_age(struct Files data[ ] , int num){

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    In function f_age you should be passing a pointer since you are changing the argument's fields in that function. You are doing that correctly in main() but your f_age header is incorrect. Add the * symbol before the data argument to make it a pointer. Also, change the . (dot) to -> arrow when accessing the members in the scanf statement.

    Alternatively, if you want to use arrays you can make the argument [].

    Also, please note that there is no need to cast malloc unless you are compiling in C++.
    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.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    81
    Thank you very muck...something else...do you know how can i change my profil name?

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Alternatively, if you want to use arrays you can make the argument [].
    Can you explain what it is ???

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    81
    If i am correct, [ ] means array! There are two ways to show that sth is an array:
    1) A[ ];
    2) *A;

    However, i don' t understand the "->" ! Having only the " *A " is not enough? Can sb explain this?

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If the variable for which you are accessing members is a pointer, -> must be used. If it is not, . must be used.

    example:

    Code:
    struct something{
       int x;
    };
    
    struct something *my_ptr_to_struct = malloc(sizeof(struct something));
    my_ptr_to_struct->x = 3;
    
    struct something my_struct;
    my_struct.x = 3;
    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.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    p->a is just syntactic sugar for (*p).a (note you need parenthesis because operator precedence)

    Food for thought.
    Last edited by Bayint Naung; 05-30-2010 at 08:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Help me with these errors... :-(
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2003, 08:18 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM