Thread: Array of strings

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    Array of strings

    Hi all!

    I'm a complete nub when it comes to C...I'm learning it as part of my course. I am having trouble on a self learning excercise, and I found another thread about array of strings but it's closed.

    Anyway, what I'm trying to do call a sub function from my main function (while passing the array defined in the main function as an argument).

    The subfunction then takes that parameter and fills the array via pointers so my main function looks nice and neat and gets all the data it needs.

    When I try running it I get the following error on line 28:
    error #2140: Type error in argument 1 to 'getnames'; expected 'char * (*)[40]' but found 'char (*)[40]'.

    but and I think it's trying to say I've got a data type error but I can't figure out where I'm going wrong. It's probably just a really simple error but I appreciate your wisdom to correct any lines of faulty code!

    Below is a sample of my code so far:

    Code:
    #include<stdio.h>
    #define n_max 13
    #define max 40
    
    char getnames(char* n[n_max][max])
    
    {
        int i, temp;
    
        printf("Please enter names\n");
        for (i=0; i<n_max; i++)
        {
            temp = n_max-i;
            scanf("%s", *n[i]);
            printf("%d-1 names remaining\n",temp);
            i++;
        }
    
        return(0);
    }
    
    int main (void)
    
    {
        int i = 0;
        char names[n_max][max];
    
        getnames(names);
    
        for (i=0; i<n_max; i++)
        {
            printf("%s\n", names[i]);
            printf("completed\n");
        }
    
        getchar();
        getchar();
    
        return(0);
    
    }
    thanks in advance

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I have put notes through your getnames function.
    Code:
    char getnames(char* n[n_max][max]) << Get rid of *
    
    
    {
        int i, temp;
    
    
        printf("Please enter names\n");
        for (i=0; i<n_max; i++)
        {
            temp = n_max-i;
            scanf("%s", *n[i]); << Get rid of *
            printf("%d-1 names remaining\n",temp);
            i++;  << Get rid of this
        }
    
    
        return(0);
    }
    Fact - Beethoven wrote his first symphony in C

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Get rid of the asterisks in getnames, both in the function header and the scanf call. It should be declared like one of these:
    Code:
    void getnames(char n[n_max][max])
    
    void getnames(char n[][max])
    
    void getnames(char *n[max])
    The leftmost dimension becomes a pointer when passing an array, so it's size is not actually needed.


    Also, you're incrementing i twice; once in the for statement and once at the end of the for block.

    The function should be declared as returning void, not char, and you should ditch the return statement.

    And you could put n_max-i in the printf statement and get rid of temp if you want.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, note that it would be better to use fgets instead of scanf to avoid a buffer overrun.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Awesome, working like a charm now, many thanks to all for your input!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-16-2012, 06:08 AM
  2. Printing an array of strings until array == NULL
    By mikemhz in forum C Programming
    Replies: 10
    Last Post: 11-04-2011, 01:09 PM
  3. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  4. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  5. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM