array

This is a discussion on array within the C Programming forums, part of the General Programming Boards category; quick question something about array.. when i put name[30].. what it actually means?the 30.. izzit i can have 31 character ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    77

    array

    quick question something about array..

    when i put name[30].. what it actually means?the 30.. izzit i can have 31 character inside it?.. if so when i type name like name: john ian something then the then row which matrix needed 2 be key in will be skipped..

    EG normal case:
    Name: John
    Matrix: bk110110
    sex: Male

    ABnormal case:
    Name: John ian ian
    Matrix: Sex:Male

    i cant key in the matrix

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    name[30] means you have declared an array with a length of 30. If this array is of type char, then it can hold 30 characters - not 31.

    I'm not sure I understand the second part of your question. Could you post some code which demonstrates the problem you are having?

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by bithub View Post
    name[30] means you have declared an array with a length of 30. If this array is of type char, then it can hold 30 characters - not 31.

    I'm not sure I understand the second part of your question. Could you post some code which demonstrates the problem you are having?
    Code:
    #include<stdio.h>
    
    void readinput(int i);
    void writeoutput(int i);
    
    typedef struct {
    	char matrix[30];
    	char name[30];
    	int  age;
    	char sex[4];
    } record;
    
    record students[10];
    
    
    
    main(void)
    {
    	int i, n, j, average;
    	printf("how many student");
    	scanf("%d", &n);
    		
    	for (i = 0; i < n; ++i){
    readinput(i);
    	}
    
    
    void readinput(int i)
    {
        printf("\nStudents no. %d\n", i+1);
        printf("	Name	");
        getchar(students[i]);
        printf("	Matrix	");
        scanf("%s", students[i].matrix);
        printf("	sex	");
        scanf("%s", &students[i].sex);
        printf("	Age	");
        scanf("%d", &students[i].age);
        return;
    {
    if i have spacing.. then the second input(matrix) will be skipped

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, it means you can have 29 characters plus the terminating zero. If you have 31 characters, you have gone 2 characters over.

    The other part of the problem is that scanf() stops as soon as it sees a blank/newline/tab (or other "whitespace").
    There are two possible solutions:
    Change the format string of your "%s" to "%29[^\n]" - that means "accept up to 29 of the characters specified in the brackets. The ^ means "invert", so we are basically saying "accept any input except (or up to) newline".

    The second option is to use fgets(string, size, stdin) - this reads a line of text, and you can use sizeof or some such to to define "size" - it will not read beyond size. You will have to remove the newline at the end of the string tho', as fgets() reads UP AND INCLUDING the newline. scanf() leaves newlines until the next scanf() - which also means you have to get rid of the OLD newline that is from the previous scanf() before using fgets().

    You can check the FAQ for "how to clear the input buffer".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by matsp View Post
    No, it means you can have 29 characters plus the terminating zero. If you have 31 characters, you have gone 2 characters over.

    The other part of the problem is that scanf() stops as soon as it sees a blank/newline/tab (or other "whitespace").
    There are two possible solutions:
    Change the format string of your "%s" to "%29[^\n]" - that means "accept up to 29 of the characters specified in the brackets. The ^ means "invert", so we are basically saying "accept any input except (or up to) newline".

    The second option is to use fgets(string, size, stdin) - this reads a line of text, and you can use sizeof or some such to to define "size" - it will not read beyond size. You will have to remove the newline at the end of the string tho', as fgets() reads UP AND INCLUDING the newline. scanf() leaves newlines until the next scanf() - which also means you have to get rid of the OLD newline that is from the previous scanf() before using fgets().

    You can check the FAQ for "how to clear the input buffer".

    --
    Mats
    thx Change the format string of your "%s" to "%29[^\n] <-- this thing works..

    btw can u tell me why i cant put %s? izzit %s a string?

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Code:
    #include<stdio.h>
    #include<string.h>
    
    void readinput(int i);
    void writeoutput(int i);
    
    typedef struct {
    	char name[80];
    	char matrix[80];
    	int  age;
    	char sex[6];
    } record;
    
    record students[30];
    
    main(void)
    {
        int i, n, maleaverage, femaleaverage,malesum = 0, femalesum = 0, mcount = 0, fcount = 0, bfcount = 0, bmcount = 0;
        printf("how many student");
        scanf("%d", &n);
    
        for (i = 0; i < n; ++i) {
            readinput(i);
        }
    
        for (i = 0; i < n; ++i)
        {
            if (strcmp(students[i].sex, "male") == 0){
                malesum = malesum + students[i].age;
            ++mcount;
    		}
    
            if (strcmp(students[i].sex, "female") == 0){
                femalesum += students[i].age;
            ++fcount;
    		}
    
        }
    
        maleaverage = malesum/mcount;
        femaleaverage = femalesum/fcount;
    
    	for(i = 0; i < n; ++i){
    		if (strcmp(students[i].sex, "male") == 0){
    			if ( students[i].age < maleaverage){
    				++bmcount;
    			}
    		}
    		if (strcmp(students[i].sex, "female") == 0){
    			if (students[i].age < femaleaverage){				
    				++bfcount;
    			}
    		}
    	}
    		
        for (i = 0; i < n; ++i){
            writeoutput(i);
        }
    	
        printf("\n Average male age %d",maleaverage);
    	printf("\n Below Average of male age have %d students", bmcount);
        printf("\n Average female age %d\n", femaleaverage);
    	printf("\n Below Average of female age have %d students\n", bfcount);
    
    }
    
    void readinput(int i)
    {
        printf("\nStudents no. %d\n", i+1);
        printf("	Name	");
        scanf(" %29[^\n]", students[i].name);
        printf("	Matrix	");
        scanf("%s", students[i].matrix);
        printf("	sex	");
        scanf("%s", &students[i].sex);
        printf("	Age	");
        scanf("%d", &students[i].age);
        return;
    }
    
    void writeoutput(int i)
    {
        printf("\nName	%s", students[i].name);
        printf("\n Matrix %s", students[i].matrix);
        printf("\n Sex %s", students[i].sex);
        printf("\n Age %d\n", students[i].age);
        return;
    }
    my exe crashed when i only enter 1 type of sex..i think is becuz of

    Code:
    printf("\n Average male age %d",maleaverage);
    	printf("\n Below Average of male age have %d students", bmcount);
        printf("\n Average female age %d\n", femaleaverage);
    	printf("\n Below Average of female age have %d students\n", bfcount);
    how do i modified this part into
    Code:
    if(strcmp(students[i].age, "female) == 0)
    rintf("\n Average female age %d\n", femaleaverage);
    	printf("\n Below Average of female age have %d students\n", bfcount);
    [/CODE]

    but i dont want use the for( i = 0; i < n; ++i) because i only want print out 1 general average.. i dun wan every students oso have 1 average.. so what shud i do?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    just check if fcount/mcount is zero before doing the calculation.

    As to why %s doesn't work: it's part of how scanf is designed. It reads a string TO THE FIRST WHITESPACE (space, tab, newline, newpage).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by matsp View Post
    just check if fcount/mcount is zero before doing the calculation.

    As to why %s doesn't work: it's part of how scanf is designed. It reads a string TO THE FIRST WHITESPACE (space, tab, newline, newpage).

    --
    Mats
    ic.. so how do i set the %29[^\n] to infinite length?

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by archriku View Post
    ic.. so how do i set the %29[^\n] to infinite length?
    But you don't have an infinite length buffer!
    Are you trying to create a buffer overrun?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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