Search code examples
cpointer-to-pointer

int ** array changing value


So I have an int **. It contains pixel values. What I'm trying to do is change the value of a set number of pixel values; say the value is 90, I want to change it to the max level that it has to create a black edge of x amount of pixels. I've been messing around with it for the last hour and can't seem to figure out how to change the actual value inside the **

This is what I have so far:

int pgmDrawEdge( int **pixels, int numRows, int numCols, int edgeWidth, char **header ){
    int i=0, j=0, count=0;

    int intensity=atoi(header[2]);

    while(count<edgeWidth){
        for(; i<numRows; i++){
            for(; j<numCols; j++){
                pixels[i][j]=intensity;
            }
        }
        count++;
    }

    return 0;
}

This is the main function call: pgmDrawCircle(pixel, rows, cols, circleCenterRow, circleCenterCol, radius, header);

I think I have the right idea, but like I mentioned above I just can't seem to figure out how to get the value to change.

I don't think I do, but do I need to allocate memory? It's being passed in from the main so I don't think I would need to since it is already full...correct?

As per request here is pgmRead:

int ** pgmRead( char **header, int *numRows, int *numCols, FILE *in  ){
    int i, j, temp, intensity;

    fgets(header[0], maxSizeHeadRow, in);
    fgets(header[1], maxSizeHeadRow, in);

    fscanf(in, "%d %d", numCols, numRows);
    fscanf(in, "%d", &intensity);
    sprintf(header[2], "%d", intensity);

    int **ptr=(int **)malloc(*numRows*sizeof(int *));
    for(i=0; i<*numRows; i++){
        ptr[i]=(int *)malloc(*numCols*sizeof(int));
    }

    for(i=0; i<*numRows; i++){
        for(j=0; j<*numCols; j++){
            fscanf(in, "%d", &temp);
            ptr[i][j]=temp; 
        }
    }

    fclose(in);
    return ptr;
}

In the main function:

    int **pixel=NULL;
    pixel=pgmRead(header, &rows, &cols, in);

if(strcmp(argv[1],"-e")==0){
    printf("Edge Drawing\n");
    pgmDrawEdge(pixel, rows, cols, edgeWidth, header);
}

Solution

  • your pgmDrawEdge pretty much works as this test shows. So problem is elsewhere

    #include <stdio.h>
    #include <stdlib.h>
    int pgmDrawEdge( int **pixels, int numRows, int numCols, int edgeWidth, char **header ){
        int i=0, j=0, count=0;
    
        int intensity=atoi(header[2]);
    
        while(count<edgeWidth){
            for(; i<numRows; i++){
                for(; j<numCols; j++){
                    pixels[i][j]=intensity;
                }
            }
            count++;
        }
    
        return 0;
    }
    
    int main() {
     char *header[]={"abc","def","90"};
     int *x[2];
     int l1[]={1,2,3,4};
     int l2[]={5,6,7,8};
     x[0]=l1;
     x[1]=l2;
    pgmDrawEdge(x,2,4,99,header);
    printf("result is %d should be %s", x[0][0], header[2] );
        return(0) ;
    }