Thread: Any one that can help?

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

    Any one that can help?

    Hey, i want to ask something: in the red points do we need " & "? and why?

    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include <string.h>
    
    #include <time.h>
    
    
    
    struct school{
    
            int age;
    
            int name[30];
    
    };
    
    
    void fuc_age(struct school A, int z){
    	int i, n;	
    		/* Initializing age */    		    
    		for(i=0; i<n; i++){
    
                 		printf("Type your age:\n");
    
                 		scanf("%d", &A[i].age);
    
        		}
    }
    
    
    void fuc_name(struct school A, int z){
    	int i, n;
    		for(i=0; i<n; i++){
    			printf("Type your name:\n");
    			scanf("%s", &A[i].name);
    		}
    }
    
    int main(void){
    
    	
    	int i, n;
    
    	struct school *A;
    
        
    
        		printf("Please type n:\n");
    
        		scanf("%d", &n);            
    
    
        		A = (struct school *)malloc(n*sizeof(struct school));
    
    
    		/* Print the A... */
    
        		for(i=0; i<n; i++){
    
                 		printf("Name: %s, Age: %d\n", A[i].name, A[i].age);
    
        		}
    
        
    
        	//system("pause");
    
        	return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    scanf() needs the address of a variable, if it is to change the value of that variable. Contrast this with printf(), which doesn't need the address of a variable, because it merely prints it, and doesn't change it in any way.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    81
    this is the output if i put the adresses:
    file4.c: In function ‘fuc_age’:
    file4.c:16: error: subscripted value is neither array nor pointer
    file4.c: In function ‘fuc_name’:
    file4.c:24: error: subscripted value is neither array nor pointer
    file4.c: In function ‘main’:
    file4.c:40: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int *’

    The A[i].age isn' t int?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Presuming you intend to submit a struct via a parameter to get filled in, you would want those function prototypes to be:

    Code:
    void fuc_age(struct school *A, int z);
    void fuc_name(struct school *A, int z);
    indicating a pointer. With that you would use:
    Code:
    scanf("%d", &(A->age));
    scanf("%s", A->name);
    In the first case, struct school member age is an int, so we have the address of operator (&) and the int in question, A->age. -> is "indirect" or pointer notation (since A is a pointer).

    In the second case A->string is itself a pointer (or rather, an array name, but it serves as a pointer here), so no & is needed.

    You have some problems in those function definitions:
    Code:
    void fuc_name(struct school A, int z){
    	int i, n;
    		for(i=0; i<n; i++){
    n is not initialized to any particular value.
    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

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    81
    I just found one more mistake...but there is one problem...
    output:
    file4.c: In function ‘fuc_name’:
    file4.c:24: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[30]’

    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include <string.h>
    
    #include <time.h>
    
    
    
    struct school{
    
            int age;
    
            char name[30];
    
    };
    
    
    void fuc_age(struct school A[], int z){
    	int i, n;	
    		/* Initializing age */    		    
    		for(i=0; i<n; i++){
    
                 		printf("Type your age:\n");
    
                 		scanf("%d", &A[i].age);
    
        		}
    }
    
    
    void fuc_name(struct school A[], int z){
    	int i, n;
    		for(i=0; i<n; i++){
    			printf("Type your name:\n");
    			scanf("%s", &A[i].name);
    		}
    }
    
    int main(void){
    
    	
    	int i, n;
    
    	struct school *A;
    
        
    
        		printf("Please type n:\n");
    
        		scanf("%d", &n);            
    
    
        		A = (struct school *)malloc(n*sizeof(struct school));
    
    
    		/* Print the A... */
    
        		for(i=0; i<n; i++){
    
                 		printf("Name: %s, Age: %d\n", A[i].name, A[i].age);
    
        		}
    
        
    
        	//system("pause");
    
        	return 0;
    
    }

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nullifyed View Post
    ...but there is one problem...
    Hopefully you noticed my last post which should address that issue.
    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

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    81
    Can i ask something..." -> " is nessecary? i mean if we do not use this and use sth else insteed, can the code be right, compile and run?

    Add: I noticed the last post now and thank you it compiled ! we were writing simultaneously...i still can' t get in my mind this " ->"...and i also have some other questions:
    Which is the difference between the 2 red points? i know that we can' t use both of them simultaneously...so i want you to tell me when we use [ ] and when *?
    Code:
    fuction(struct example *A[] ){
    also in this code, can you explain the diference:
    1) scanf("%d", &(A[i].age));
    2) scanf("%d", &(A->.age)); where is " i "? is it correct ? the point A direct only in the first cell af the array...right? so what happens with the rest?

    Code:
    scanf("%d", &(A->age));
    scanf("%s", A->name);
    Last edited by nullifyed; 05-30-2010 at 04:34 PM.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    use sth else insteed,
    like what?

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    81
    Quote Originally Posted by KBriggs View Post
    like what?
    such as &... wrong?

    insteed of:
    Code:
    scanf("%d", &(A->age));
    if i write:
    Code:
    scanf("%d", &A[i].age);
    what will happen apart from the error? i mean, what exactly did i wrote and how the scanf will operate?

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nullifyed View Post
    what will happen apart from the error? i mean, what exactly did i wrote and how the scanf will operate?
    If it is an error, what will happen apart from that does not matter too much. If it compiles and works, I guess you should be able to find evidence of something.
    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
    81

    Smile

    Quote Originally Posted by nullifyed View Post
    Can i ask something..." -> " is nessecary? i mean if we do not use this and use sth else insteed, can the code be right, compile and run?

    Add: I noticed the last post now and thank you it compiled ! we were writing simultaneously...i still can' t get in my mind this " ->"...and i also have some other questions:
    Which is the difference between the 2 red points? i know that we can' t use both of them simultaneously...so i want you to tell me when we use [ ] and when *?
    Code:
    fuction(struct example *A[] ){
    also in this code, can you explain the diference:
    1) scanf("%d", &(A[i].age));
    2) scanf("%d", &(A->.age)); where is " i "? is it correct ? the point A direct only in the first cell af the array...right? so what happens with the rest?

    Code:
    scanf("%d", &(A->age));
    scanf("%s", A->name);
    i 'll be glad if somebody could explain these to me...

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    There's nothing to explain really, I think I provided you with an example in another thread. If you are using a POINTER to a struct you need to access its members using the little arrow -> . In your case A is an array of structs so you would access each member like so: A[i].age . However, if you had an array of pointers to structs you would use A[i]->age, REPLACING the DOT with ARROW.

    No, there is no way around this unless you want to mess around with defines which doesn't seem like a good idea. Why don't you want to use the arrow; if anything it makes the code more readable because you know that variable is a pointer without looking at the declaration.
    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.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    81
    So, if i am right, the only change that i have to made is the red point.
    Code:
    void fuc_age(struct school A[ ], int z){
    and inside the fuction, this is ok...
    Code:
    scanf("%d", &A[i].age);
    scanf("%s", A[i].name);
    Am i wrong?

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You have to Google and read about the difference between pointers and arrays either on this site or somewhere else. I feel like this will help you out understanding the source of your confusion a little better.

    As for the change, assuming that you are adding what is in red, then yes that should work.
    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.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nullifyed View Post
    where is " i "? is it correct ? the point A direct only in the first cell af the array...right? [...]i 'll be glad if somebody could explain these to me...
    Gosh, that was my fault for reading too quickly, sorry. The i can go where it was, so:
    Code:
    scanf("%d", &(A[i]->age));
    scanf("%s", A[i]->name);
    You still don't need to do this, however:
    Code:
    fuction(struct example *A[] ){
    Just plain struct example *A is fine. A could be an array, just like char example *A could be (usually is) and int example *A could also point to either a single int or an array of them.
    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

Popular pages Recent additions subscribe to a feed