CS student
CS student

Reputation: 33

C - how to use strcpy for a 2 dimensional array?

I'm trying to build a 2 dimensional array by using str.cpy, but the program fails. The code receives 4 arrays add copies their content to their matching arrays.

  int InsertStudent(char *firstName, char* lastName, char* dynCourses, char *dynGrades,
        char firstNames[50][20],
        char familyNames[50][20], char courses[50][5][20],
        char grades[50][5])
    {
        int set, 
        int cset = 0;
        for (set = 0; set <= (50); set++)
        {
            if (firstNames[set][cset] == '\0')
            {
                strcpy(firstNames[set][cset], firstName);
                strcpy(familyNames[set], lastName);
                for (cset = 0; cset <= 5; cset++)
                {
                    strcpy(courses[set], dynCourses);
                    strcpy(grades[set], dynGrades);
                }

            }
        }
        return 0;
    }

Upvotes: 1

Views: 11942

Answers (1)

user2736738
user2736738

Reputation: 30926

  • Well clearly the error is using strcpy wrongly.

    The correct way would be

    strcpy(firstNames[set], firstName);
    
  • Also in the loop it should be

        for (cset = 0; cset < MAX_COURSES; cset++)
        {
            strcpy(courses[cset], dynCourses);
            strcpy(grades[cset], dynGrades);
        }
    

    Note that the idiomatic C loop is for (int i = 0; i < MAX; i++), using < and not <=.

The signature of the strcpy function is

 char *strcpy(char * restrict s1, const char * restrict s2);

Earlier you passed in place of s1 a char instead of char*. You must have got some warning (if enabled). If not then turn all compiler flags -Wall -Werror.

             if (firstNames[set][cset] == '\0')

But if you are initially checking an uninitilized value with \0. This will higly unlikely will turn out to be false. There is no gurantee that the char array which doesn't contain strings will be initialized with 0 automatically. So make sure you have initialized the char arrays in the callee function like this

char arr[20][50]={0};
  • The loop is from 0 to MAX_STUDENTS. You are invoking an undefined behavior looping on array index out of bound if MAX_STUDENTS is greater than or equal to 50. Same goes for MAX_COURSES. More clearly the looping would be for (set = 0; set < (50); set++).

  • Again there will be a lot better way to deal with it if you put the initialization and copy part seperate. Otherwise this will be uneasy to maintain.

  • Seeing your use courses it is obvious that you want to declare it like this

    char courses[5][20];
    

same goes for the grades array. Here you were trying to copy a string into into a 2d array. Compiler would complain about type incompatibility.

  • Also in the function you didn't return anything meaningful. The correct way would be to return the index value on which new name or information is added.

  • Here you are copying content of dynGrades and dynCourses to the array. So they will all contain the same values. Is this what you want? Because then what's the use of keeping 5 seperate char arrays - one could serve the purpose pretty well.

Upvotes: 4

Related Questions