2

how do i correct this

i didn't use structure intentionally this is a program to input student's name, subject and marks. in the last block, the array (subject+f) 's 1st subscript is returning garbage values while the rest subscript are returning desired result. i have also posted the image of output as link.

#include<stdio.h>
#include<string.h>
int main()

{

int size,i,k,sub,a=0,reference;


    int temp,sorted;
    char temp_s[10];
    char temp_sb[10];

    printf("enter the size of class\n");
    scanf("%d",&size);
    printf("how many subjects are there?\n");
    scanf("%d",&sub);
    reference = sub;
    char name[size][20];
    char subject[size*sub][20];
    int marks[sub*size];
    int total,subtotal,retotal;
    for(k=0;k<sub;k++)
    {
    printf("so what's  the no. %d subject\n",k+1);
    scanf(" %s",(subject[k]));
    }

    for(i=0;i<size;i++)
        {

            int j,k=0;
            printf("Enter a name of student %d\n",i+1);
            scanf(" %s",(name+i));


                for(j=a;j<reference;j++)
                {
                    printf("enter marks of %s\n",(subject[k]));
                    scanf("%d",(marks+j));
                    k++;
                }

                a=j;
                reference=sub+j;


        }

    reference=sub;
    a=0;


    printf("\n list of students and marks:\n");
    for(i=0;i<size;i++)
        {
            int j,f=0;
            printf("%s\n",(name+i));
            for(j=a;j<reference;j++)
            {
                 printf("%s %d\n",(subject[f]),(marks[j]));
                 f++;
            }
            a=j;
            reference=sub+j;

        }
}
3
  • 1
    I suggest you take a debugger and step thru the program. There is definitely something wrong with your indexes. Commented Feb 21, 2018 at 9:10
  • Is any student or subject name more than nine characters? Commented Feb 21, 2018 at 9:17
  • Why don't you use subject[f]? I think subject is a char** and you want to have a char*... The two dimensional array is a Pointer to a Pointer. While you want to write in a char*. Commented Feb 21, 2018 at 9:18

2 Answers 2

2

Besides the problem with length of names and subjects, this here is a major problem:

(subject+k)

You are probably misunderstanding the subject[k] and *(subject + k) equivalent.

The variable subject is an array of arrays. That means subject[i] is an array (of char and can be used as a zero-terminated string).

The expression (subject + k) is a pointer to the array in subject[k]. It's equal to &subject[k] which have the type char (*)[10]. It's can not be used as a zero-terminated string without dereferencing. So either use *(subject + k) or the simple, less-to-write and easier-to-read subject[k].

Sign up to request clarification or add additional context in comments.

1 Comment

i understand but it doesnt seem to have any difference to the result.. though i corrected it.
0

I think you also need to change

int marks[sub];

to

int marks[size * sub];

one mark for each subject for each student, correct?

6 Comments

it works now , but the code for(j=a;j<reference;j++) { printf("enter marks of %s\n",(subject[k])); scanf("%d",(marks+j)); k++; } returns one extra loop when i take subject and size more than 2.
can you update the question with your current complete code?
i updated it. it works fine as long as i not enter the size more than 2.
add this debug statement just before you prompt for the marks and I think you will see what is going on printf("%d..%d\n", j, reference);
thankyou. the problem was with reference=reference + j; . so i changed it to reference=sub+j
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.