cLanguage problem

Asked 2 years ago, Updated 2 years ago, 45 views

It's a language problem Using a two-dimensional integer array, input and output a 3X3 matrix, and create an appropriate function for the first row It's a question to print out after exchanging two lines I was typing ab c def g, but it stopped working.

Warning is .warning C4047: 'function': 'char **' has a different indirect reference level than 'char [3][3]'. warning C4024: 'change_array': format and actual parameter 1 have different formats. I don't know what's wrong with it.


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


void change_array(char **a)
{
    char *temp = malloc(sizeof(char)*3); ;
    for (int i = 0; i <= 2; i++) {
         *(temp+i) = *(*(a + i));
        *(*(a + i)) = *(*(a+i)+1);
        *(*(a + i) + 1) = *(temp + i);
    }
}
int main()
{
    int i, j,b,c;
    char a[3][3];
    for (i = 0; i <= 2; i++) {
        for (j = 0; j <= 2; j++) {
            scanf("%c ", &a[i][j]);
        }
    }
    change_array(a);
    printf("array is changed\n");
    for (b = 0; b < 3; b++) {
        for (c = 0; c < 3; c++) {
            printf("%c", a[b][c]);
        }
        printf("\n");
    }

}

c pointer array

2022-09-22 08:16

1 Answers

There's a concept called the first IO BUFFER.

When you receive an input as a scanf function, it is stored in a place called IO buffer, and when you enter, you enter the value and type the enter key.

The enter key value is then saved at the next scanf.

It's not the result you want...So I need a code to empty the buffer.

The second array is a constant. It means you can't change it.

Reassigning an array element in the change_array function will result in segment faults.

The third arrangement and the pointer are clearly different. For that reason, the compiler issues a warning.

I made simple modifications to the parts that are needed as much as possible.

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


void change_array(char **a)
{
    char *temp = malloc(sizeof(char)*3); ;
    for (int i = 0; i <= 2; i++) {
         *(temp+i) = *(*(a + i));
        *(*(a + i)) = *(*(a+i)+1);
        *(*(a + i) + 1) = *(temp + i);
    }
}
int main()
{
    int i, j,b,c;
    //char a[3][3];
    // 2D pointer initialization
        char **a = (char**)malloc(sizeof(char) * 3);
        for(int index = 0; index < 3; index++){
                a[index] = (char*)malloc(sizeof(char) * 3);
        }

    for (i = 0; i <= 2; i++) {
        for (j = 0; j <= 2; j++) {
            scanf("%c", &a[i][j]);
                        getchar(); //<- Correct. Clear Buffer
        }
    }

    change_array(a);
    printf("array is changed\n");
    for (b = 0; b < 3; b++) {
        for (c = 0; c < 3; c++) {
            printf("%c", a[b][c]);
        }
        printf("\n");
    }

}

Tests and Results

gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)

allinux@ip-10-0-0-29:~$ gcc -std=c99 -o sample sample.c
allinux@ip-10-0-0-29:~$ ./sample
1
2
3
4
5
6
7
8
9
array is changed
213
546
879


2022-09-22 08:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.