我無法打印出存儲在陣列中的值。它似乎在打印出內存地址。這是我的代碼:C - 爲什麼我一直打印出內存地址而不是陣列中的值
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void getMatrix(int x, int y);
void printMatrix(int arr[], int x, int y);
int product(int arr1[], int arr2[]);
int main(void){
//Variables that will store matrix size
int m, n, o, p;
//Prompt user for size of Matrix A
printf("Enter the rows and columns of Matrix A with space in between: ");
//Read input
scanf("%d %d", &m, &n);
//Prompt user for the size of Matrix B
printf("Enter the rows and columns of Matrix B with space in between: ");
//Read input
scanf("%d %d", &o, &p);
//Seed RND Generator
srand(time(NULL));
//Check input
if(n != o){
while(n != o){
printf("Matrix Sizes are not valid. Please enter valid sizes for the Matrices: ");
scanf("%d %d %d %d", &m, &n, &o, &p);
}
}
//Function Calls
printf("Matrix 1:\n");
getMatrix(m, n);
printf("\nMatrix 2:\n");
getMatrix(o, p);
}
void getMatrix(int x, int y){
//Counter
int c;
//Size Declaration
int size = x * y;
//Array Declaration
int arr[size];
for(c = 0; c < size; c++){
arr[c] = rand()%10;
}
printMatrix(arr[size], x, y);
}
void printMatrix(int arr[], int x, int y){
//Counters
int i, j;
for(i = 0; i < y; i++){
printf("\n");
for(j = 0; j < x; j++){
printf("%d ", arr[j]);
}
}
}
因此,基本上這個代碼應該採取輸入,並創建一個可變長度數組和它應該隨機數存儲在一個1個維陣列,然後它們在要打印輸出二維數組或矩陣的形式。我覺得printMatrix
函數的參數或者在傳遞getMatrix
函數中獲得的數組時可能有問題。任何幫助將不勝感激,謝謝。
編輯:謝謝大家的幫助。我甚至沒有考慮將其作爲解決方案。但它現在起作用並打印出它應該的數字。再次感謝
我相信編譯器在你抱怨,你忽略它的投訴,其努力是有幫助的。它抱怨在這行'printMatrix(arr [size],...' – pm100
嘗試更改'printMatrix(arr [size],x,y);''printMatrix(arr,x,y);' – bruceg
您應該得到'printMatrix(arr [size],x,y);'。如果你不這樣做,第一件事就是調整你的編譯器設置,直到你得到一個錯誤,然後修復錯誤 –