gpt4 book ai didi

c - 指针数组和指针数组的内存分配

转载 作者:行者123 更新时间:2023-12-04 02:18:40 28 4
gpt4 key购买 nike

下面是我坚持的代码部分。我想为

动态分配内存
  • 指向数组的指针
  • 指针数组

我收到几条错误消息,例如从 int * 到 int 的无效转换等等。

指向数组的指针

int (*array)[nrows][ncolumns];
array = (int*)malloc(nrows * ncolumns * sizeof(int));


printf("\n Enter the elements:\n");

for(i=0; i<nrows; i++)
{
for(j=0; j<ncolumns; j++)
{
scanf("%d", array[i][j]);
}
}

printf("Entered array is :\n\n");

for(i = 0;i<nrows; i++)
{
for(j = 0; j<ncolumns; j++)
{
if(j== ncolumns-1)
{
printf("%d \n", *array[i][j]);
}
else
{
printf("%d", *array[i][j]);
}

指针数组

int *array[nrows][ncolumns];
array[nrows][ncolumns] = (int*)malloc(nrows * ncolumns * sizeof(int));

printf("Enter elements:\n");

for(i = 0; i<nrows; i++)
{
for(j = 0; j<ncolumns;j++)
{
scanf("%d",&array[i][j]);


}
}


printf("Entered array is: \n");
for(i = 0; i<nrows; i++)
{
for(j = 0; j<ncolumns;j++)
{
if(j == ncolumns-1)
{
printf("%d \n",array[i][j]);
}
else
{
printf("%d \t",array[i][j]);
}


}
}

最佳答案

1>指向数组的指针

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

int main(void){
int nrows = 3;
int ncolumns = 4;
int i, j;

int (*array)[nrows][ncolumns];//do you want <<int (*array)[ncolumns]>> ?
//like as int src[nrows][ncolumns]; array = &src;
array = malloc(nrows * ncolumns * sizeof(int));//(int*) : type mismatch

printf("\nEnter the elements:\n");

for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
scanf("%d", &(*array)[i][j]);
}
}

printf("Entered array is :\n\n");

for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
if(j != 0)
putchar(' ');
printf("%d", (*array)[i][j]);//need ( )
}
putchar('\n');
}
free(array);
return 0;
}

2>指针数组

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

int main(void){
int nrows = 3;
int ncolumns = 4;
int i, j;

int *array[nrows][ncolumns];
int *src = (int*)malloc(nrows * ncolumns * sizeof(int));//no need (int*)

printf("Enter elements:\n");

for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns;j++){
array[i][j] = &src[ i * ncolumns + j];//pointer pointed to entity (src[ i * ncolumns + j])
scanf("%d", array[i][j]);//type of array[i][j] is int *
}
}

printf("Entered array is: \n");
for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
if(j != 0)
putchar(' ');
printf("%d", *array[i][j]);//need * for dereference
}
putchar('\n');
}

free(src);
return 0;
}

3>选项

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

int main(void){
int nrows = 3;
int ncolumns = 4;
int i, j;

int (*array)[ncolumns];
array = (int (*)[ncolumns])malloc(nrows * sizeof(*array));//sizeof(*array) : sizeof(int[ncolumns])

printf("\nEnter the elements:\n");

for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
scanf("%d", &array[i][j]);
}
}

printf("Entered array is :\n\n");

for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
if(j != 0)
putchar(' ');
printf("%d", array[i][j]);
}
putchar('\n');
}
free(array);
return 0;
}

关于c - 指针数组和指针数组的内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32561023/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com