so im working on a mid-term exam average program of a class of 10 students. would my code start as follows?

void getnames(char stuname[10][20])
{
 int ctr;

 for(ctr1=0;ctr1<10;ctr1++)
 {
  printf("Enter Name %d ", ctr);
  scanf("%s",stuname[ctr]);
 }

void printnames(char stuname[10][20])
{
 int ctr2;

 for(ctr2=0;ctr2<10;ctr2++)
 {
  printf("%s\n", stuname[ctr2]);
 }

and have getNAME as a global function?
Hope my question makes sense. Im still in the pseudocode phase

Dani AI

Generated

A few quick, practical clarifications based on the snippet and the replies from , and :

The snippet has small syntax/logic issues (mismatched loop variable names and a missing brace), and it uses fixed magic numbers in the parameter list. Define sizes with macros and pass the array plus an explicit count so the functions stay reusable. Reading names with scanf("%s",...) loses spaces and risks overflow; use fgets and strip the newline. As noted, put main helper functions (or at least their prototypes) above main so the program is easy to follow. Contrary to ’s comment, arrays decay to pointers when passed—modifying array elements inside the function affects the caller (this is why passing the size is also important).

Example of a minimal, safer setup:

#define NUM_STUDENTS 10
#define NAME_LEN 20  /* usable characters, space for NUL with +1 below */

void get_names(char names[][NAME_LEN+1], int n);
void print_names(const char names[][NAME_LEN+1], int n);

int main(void) {
    char names[NUM_STUDENTS][NAME_LEN+1];
    get_names(names, NUM_STUDENTS);
    print_names(names, NUM_STUDENTS);
    return 0;
}

Safer get_names pattern (use <string.h> for strcspn):

void get_names(char names[][NAME_LEN+1], int n) {
    for (int i = 0; i < n; ++i) {
        printf("Enter name %d: ", i + 1);
        if (!fgets(names[i], NAME_LEN + 1, stdin)) names[i][0] = '\0';
        else names[i][strcspn(names[i], "\n")] = '\0';
    }
}

If each student needs a score, prefer a struct to parallel arrays:

typedef struct { char name[NAME_LEN+1]; int score; } Student;

When mixing line-based input and scanf, clear the input buffer (int c; while ((c = getchar()) != '\n' && c != EOF);) to avoid leftover newlines. Also check return values of input functions and always reserve one byte for the NUL.

Recommended Answers

All 3 Replies

While you're getting their name, don't you also want to get their exam score?

Are you going to use parallel arrays student name <==> their score sharing the same index, or what's your plan for that?

I like all the major functions to be up above main(). Easy to refer to, and all together. Only very minor one's are put inside the calling function.

You are passing the arrays by value. The changes made to these arrays will be local to the function.
Pass by pointer or use global variables

commented: Arrays can't be passed by value the way you imply. -2

Arrays are *always* passed by reference. (More accurately, the array is "degraded" (becomes just a pointer), which is then passed by copying, but since a pointer is just an address anyway, it's a pass by reference, via proxy.

That's how C always does pass by reference, btw. When we send a pointer to a function, the pointer that arrives is just a copy of the pointer that was listed in the call. The effect is the same as if the original pointer was sent, so no trouble.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.