Thread: Using realloc to change array values

  1. #1
    Registered User
    Join Date
    Dec 2021
    Posts
    34

    Using realloc to change array values

    So I've declared two arrays like this (red, green and blue are in a structure and the head pointer is just used to locate which file I'm looking at):
    Code:
    int i, j, qi, qj, identify_x, identify_y;
        FILE *fid;
        char fnameid[100];
        printf("\tEnter Input Image File Name To Perform Image detection On: \n");
        scanf("%s", fnameid);
        fseek(stdin,0,SEEK_END);
        fid = fopen(fnameid, "r");
        if (fid == NULL)
        {   
            printf("\tError while opening the file\n");
        }
        else    
        {
            printf("\tWriting in %s\n", fnameid);
        }
    
    
        fscanf(fid, "%s %d %d %d", head[1].filetype, &head[1].width, &head[1].height, &head[1].rgb_comp_colour);
        printf("%s %d %d %d", head[1].filetype, head[1].width, head[1].height, head[1].rgb_comp_colour);
    
    
        RGBArray = (PPMPixel **)malloc(head[1].height*sizeof(PPMPixel*)); //Points to malloc
        if((RGBArray == NULL))
        {
            printf("\tError allocating memory to the array\n");
        }
        else
        {
            printf("\tMemory allocated to the PPM array sucessfully\n");
        }
        for (i=0;i<head[1].height;i++)
        {
            RGBArray[i] = (PPMPixel *)malloc(head[1].width*sizeof(PPMPixel));
        }
        
        //Initialising each element
        for (j=0;j<head[1].height;j++)
        {
            for (i=0;i<head[1].width;i++)
            {
                fscanf(fid, "%d %d %d ", &q.red, &q.green, &q.blue);
                PPMPixel* data = &RGBArray[j][i];
                data->red = q.red;
                data->green = q.green;
                data->blue = q.blue;
            }
        }
    
    
        FILE *fq;
        char fnameq[100];
        printf("\tEnter Query Image File Name: \n");
        scanf("%s", fnameq);
        fseek(stdin,0,SEEK_END);
        fq = fopen(fnameq, "r");
        if (fq == NULL)
        {   
            printf("\tError while opening the file\n");
        }
        else    
        {
            printf("\tWriting in %s\n", fnameq);
        }
    
    
        fscanf(fq, "%s %d %d %d", head[2].filetype, &head[2].width, &head[2].height, &head[2].rgb_comp_colour);
        printf("%s %d %d %d", head[2].filetype, head[2].width, head[2].height, head[2].rgb_comp_colour);
    
    
        QueryArray = (PPMPixel **)malloc(head[2].height*sizeof(PPMPixel*)); //Points to malloc
        if((QueryArray == NULL))
        {
            printf("\tError allocating memory to the array\n");
        }
        else
        {
            printf("\tMemory allocated to the PPM array sucessfully\n");
        }
        for (qi=0;qi<head[2].height;qi++)
        {
            QueryArray[qi] = (PPMPixel *)malloc(head[2].width*sizeof(PPMPixel));
        }
        
        //Initialising each element
        for (qj=0;qj<head[2].height;qj++)
        {
            for (qi=0;qi<head[2].width;qi++)
            {
                fscanf(fq, "%d %d %d ", &r.red, &r.green, &r.blue);
                PPMPixel* data = &QueryArray[qj][qi];
                data->red = r.red;
                data->green = r.green;
                data->blue = r.blue;
            }
        }
    And I've located the query array in the RGB array like so:
    Code:
    int wally; //Tests if query array is found in main input array
        for (int i=0;i<(head[1].width-head[2].width);i++)
        {
            for(int j=0;j<(head[1].height-head[2].height);j++) //
            {
                for(int qi=0; qi<head[2].width; qi++)
                {
                    for(int qj=0;qj<head[2].height;qj++)
                    {
                        if ((RGBArray[i+qi][j+qj].red != QueryArray[qi][qj].red) || (RGBArray[i+qi][j+qj].green != QueryArray[qi][qj].green)
                            || (RGBArray[i+qi][j+qj].blue != QueryArray[qi][qj].blue))
                        {
                            wally = FALSE;
                            break;
                        }
                    }
                    if(wally = TRUE)
                    identify_y = j; //Stores j values that match in variable
                    identify_x = i; //Stores i values that match in variable
                    break;
                }
            }
        }
    What I want to do is use realloc to change the values withing the RGBArray so that I can have a red box around the located query array. Thank you for any help.
    Last edited by DGB_684; 01-05-2022 at 06:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change/Keep Values (C beginner)
    By Erazz in forum C Programming
    Replies: 2
    Last Post: 10-11-2014, 02:45 PM
  2. How can I change #define values without recompiling?
    By maryfsan in forum C Programming
    Replies: 5
    Last Post: 07-31-2014, 03:06 AM
  3. Problems when trying to change values in an array
    By kdun in forum C++ Programming
    Replies: 8
    Last Post: 03-30-2013, 04:24 AM
  4. rand() seeded with time but values don't change
    By mmfuller in forum C++ Programming
    Replies: 7
    Last Post: 09-21-2009, 12:41 AM
  5. Unexpected change of input values
    By imrahaseen in forum C Programming
    Replies: 6
    Last Post: 06-22-2007, 02:25 AM

Tags for this Thread