-1

I want to take dynamic array in my program. I have used malloc function. In my system there is total 32 relays are present. At a time there will be 12 relays will be off max. But in worst case it can be 32.

so I have taken size = 32 in my program. therefore i need to enter 32 values every time while testing. I want to take only 12 values at a time in array.

This is my code..

 #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>

    void main(){

    int  relay_check[]={0},i;
    int *relay_check1;
    const int  size=32;

    relay_check1 = (int *)malloc(sizeof(int)* size);

    for(i = 0;i < size ; i++ )
       {
           scanf("%d",&relay_check[i]); 
           /*relay_check[i] is the content of element at index i and &relay_check[i] is the         address of element 
     at index i */
    }
     if (!relay_check) { /* If data == 0 after the call to malloc, allocation failed  for some reason */
    perror("Error allocating memory");
    abort();
    }

    for( i = 1;i < size ; ++i) {
    printf("relay_check [%d]: %d\n", i, relay_check[i]);
    }
    for(i=1;i<9;i++){
        if(i == relay_check[i] )
            printf("0");
        else
            printf("1");
    }

    printf("\n");

    for(i=9;i<17;i++){
        if(i == relay_check[i] )
            printf("0");
        else
            printf("1");
    }

    printf("\n");

    for(i=17;i<25;i++){
        if(i == relay_check[i] )
            printf("0");
        else
            printf("1");
    }

    printf("\n");

    for(i=25;i<33;i++){
        if(i == relay_check[i] )
            printf("0");
        else
            printf("1");
    }
    printf("\n");

    getch();
    }  

so what else changes i need to do?

Kanwaljit Singh
  • 4,339
  • 2
  • 18
  • 21
Jack13
  • 7
  • 3
  • 1
    this `int relay_check[]={0}` declaration stores only one value. You need to find space to store other 31 values separately. – noufal Dec 26 '13 at 07:07
  • delete `relay_check[]={0},` and rename `relay_check1` to `relay_check`. also `if (!relay_check) {` too late. – BLUEPIXY Dec 26 '13 at 10:47

2 Answers2

0

int relay_check[]={0}; means that you are allocating space for only one integer and initializing it to 0 and hence

for(i = 0;i < size ; i++ )
{
   scanf("%d",&relay_check[i]); 
}

this will invoke undefined behavior. Declare relay_check as

 int  relay_check[32]={0}; 

And also no need to cast the return value of malloc.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
0

I'm just going to sum up what you need to change in you code :

  • when declaring relay_check you either give the exact size and initialize it later or explicitly initializing it without giving the size.

    *- the exact size : int relay_check[32]

    *- initializing without giving size : int relay_check[] = {1 , 45 , 7 , ... , 47} // the whole 32 elements

    *- OR (I think this is what you wanted to do ) give the exact size and initialize all the elements to 0s: int relay_check[32] = {0}

  • error checking concerning malloc need to be done immediately after calling it

  • after scanf() add a getchar() so that it receives the trailing newline character

Farouq Jouti
  • 1,657
  • 9
  • 15