Brogolem35
Brogolem35

Reputation: 141

C - Members of different arrays share pointers

I am new to C and I am experementing with printf, scanf functions and variables. I made a program that gets some variables from the user and stores them in 2 different arrays. But for some reason 2 members of different arrays share pointers thus values. Here is my code:

setlocale(LC_ALL,"Turkish");
double odev[2];
double quiz[2];
printf("Dönem sonu not hesaplama sistemine hoş geldiniz.\n");
for(short i=0;i <= 2;i++)//Ödev notunu 3 kez almasi için
{
    while(true)
    {
        char girdi[20];
        printf("%d. ödev notunu giriniz:",i+1);
        scanf("%s", girdi);
        bool floatKontrol = true;
        int girdiuzunluk = strlen(girdi);
        for(short j = 0;j < girdiuzunluk && floatKontrol;j++)
        {
            if(!(girdi[j] == ',' || isdigit(girdi[j])))
            {
                if(girdi[j] == '.')
                {
                    girdi[j] = ',';
                }
                else
                {
                    floatKontrol = false;
                    printf("Girmiş olduğunuz değer anlaşılamamıştır. Lütfen pozitif sayılar giriniz.\n");
                }
            }
        }

        if(floatKontrol)
        {
            odev[i] = strtod(girdi, NULL);
            printf("%f\n",odev[i]);
            if(odev[i]>100)
            {
                printf("100'ün üzerinde değer girilemez. Lütfen tekrar deneyin.\n");
            }
            else{
                break;
            }
        }
    }
}

for(short i=0;i <= 2;i++)
{
    while(true)
    {
        char girdi[20];
        printf("%d. quiz notunu giriniz:",i+1);
        scanf("%s", girdi);
        bool floatKontrol = true;
        int girdiuzunluk = strlen(girdi);
        for(short j = 0;j < girdiuzunluk && floatKontrol;j++)
        {
            if(!(girdi[j] == ',' || isdigit(girdi[j])))
            {
                if(girdi[j] == '.')
                {
                    girdi[j] = ',';
                }
                else
                {
                    floatKontrol = false;
                    printf("Girmiş olduğunuz değer anlaşılamamıştır. Lütfen pozitif sayılar giriniz.\n");
                }
            }
        }

        if(floatKontrol)
        {
            quiz[i] = strtod(girdi, NULL);
            if(quiz[i]>100)
            {
                printf("100'ün üzerinde değer girilemez. Lütfen tekrar deneyin.\n");
            }
            else{
                break;
            }
        }
    }
}
printf("%f\n",odev[0]);
printf("%f\n",odev[1]);
printf("%f\n",odev[2]);
printf("%f\n",quiz[0]);
printf("%f\n",quiz[1]);
printf("%\n",quiz[2]);

User input and console output:

> Dönem sonu not hesaplama sistemine hoÅY geldiniz.
>1. ödev notunu giriniz:23
>23,000000
>2. ödev notunu giriniz:48
>48,000000
>3. ödev notunu giriniz:31
>31,000000
>1. quiz notunu giriniz:4
>2. quiz notunu giriniz:5
>3. quiz notunu giriniz:6  
>6,000000  
>48,000000  
>31,000000  
>4,000000  
>5,000000  
>6,000000

When I make program to write the pointers of the array members output is this:

>Dönem sonu not hesaplama sistemine hoÅY geldiniz.
>1. ödev notunu giriniz:23
>23,000000
>2. ödev notunu giriniz:48
>48,000000
>3. ödev notunu giriniz:31
>31,000000
>1. quiz notunu giriniz:4
>2. quiz notunu giriniz:5
>3. quiz notunu giriniz:6  
>000000000061FDD0  
>000000000061FDD8  
>000000000061FDE0  
>000000000061FDC0  
>000000000061FDC8  
>000000000061FDD0  

My IDE: Code::Blocks 20.03

Upvotes: 0

Views: 55

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

In C, you have to specify the number of elements, not the maximum index, when you declare arrays.

You declared double odev[2];, so only odev[0] and odev[1] are available and odev[2] is out-of-range. quiz[2] is also out-of-range.

Allocate enough elements like

double odev[3];
double quiz[3];

or (if you feel this is easier to understand)

double odev[2+1];
double quiz[2+1];

to eliminate this error.

Upvotes: 4

Related Questions