我不明白为什么我必须接收2D数组的内容b[][3]而不是 **b?另外我们如何才能为二维数组调用值?另外,2D阵列的地址arr等于内容的arr等于*arr等于&arr[0][0]; 所有地址都一样.我无法清楚地看清楚它; 有人可以向我解释如何实际存储多维数组."图片有用的链接将受到欢迎".
#include "hfile.h" // contains all needed H files
void caller(int b[][3]) // why can't we write **b?
{
int k=100;
printf("\n****Caller:****\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
b[i][j]=k++;
printf("\t %d",b[i][j]);
}
printf("\n");
}
}
int main()
{
int arr[3][3]={1,2,3,4,5,6,7,8,9}; // original containts
caller(arr); // Called caller function passing containts of "arr"
printf("\n****Orignal****\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
printf("\t %d",arr[i][j]);
printf("\n");
}
return 0; …Run Code Online (Sandbox Code Playgroud) 我有两个函数来创建数组.
double *Array1D (int nx, int dsize) {
double *v;
v = calloc(nx, dsize);
return v;
}
double **Array2D (int ny, int nx, int dsize) {
double **v; int j;
for (j = 0; j < ny; j++)
v[j] = Array1D(nx, dsize);
return v;
}
int i, j;
pn = Array2D (ny, nx, sizeof(double));
for (j = 0; j < ny; j++)
for (i = 0; i < nx; i++)
pn[j][i] = 1.0 + (i + j * nx) * …Run Code Online (Sandbox Code Playgroud) 什么是分配内存以一个最好的方式two-d array中C,从两个观点:memory-management和speed?
另外,哪个更好用,a two-d array(并为其分配内存)或double pointer?有人可以详细解释一下,内部会发生什么,为什么一种方法比另一种更好?
我已经创建了这个代码来测试一个错误,我在主代码中得到了它,并且它共享同样的问题.我总是得到分段错误或损坏的数据(零或奇数).
这是代码:
int *p=NULL;
int func (int **point);
int main() {
int num = 5647;
p = malloc(sizeof(int)*2);
p[0] = num;
p[1]= 657;
printf("%d\n", p[0]);
printf("%d\n", p[1]);
func(&p);
printf("%d\n", p[0]);
printf("%d\n", p[1]);
printf("%d\n", p[2]);
printf("%d\n", p[3]);
return 0;
}
int func (int **point){
*point = realloc(*point,sizeof(int)*4);
if (*point==NULL){
printf("\n abort \n");
exit(0);
}
*point[0] = 867;
*point[1]= 777;
*point[2] = 67;
*point[3]= 77;
}
Run Code Online (Sandbox Code Playgroud)
我正在获得分段错误*point[1]=777;.如果我试图这样做,point[1]=777;我会得到错误的数据.随着任何变化int func (int **point);或func(&p);我正在进行分段故障realloc.
请指教,我已阅读有关双指针的信息,并试图按照我找到的所有解决方案,但每次我收到此错误.
我有一个C++算法,我需要在Java中实现类似的东西.我在内存分配方面遇到了麻烦.如何将以下代码段从C++迁移到Java?
size_x = 3; size_y = 6;
double **Data, *pDataData;
Data = (double **)malloc(size_x*sizeof(double *)+size_x*size_y*sizeof(double));
for (i = 0, pDataData = (double *)(Data+size_x); i < size_x; i++, pDataData += size_y)
Data[i]=pDataData;
Run Code Online (Sandbox Code Playgroud)
我知道对于一个简单的malloc,如:char*x =(char*)malloc(256); 在Java中,我会说:ByteBuffer x = ByteBuffer.allocate(250);
对于任何更复杂的事情,我感到困惑.
先感谢您
我知道为了malloc指向数组的指针数组,语法应该是:
(int**)malloc(numberOfDesiredElements*sizeof(int*))
Run Code Online (Sandbox Code Playgroud)
不小心忘了把numberOfDesiredElements*size放在前面。它搞乱了我的程序抛出随机分段错误,即有时程序输出正确,有时它出现段错误。
有人可以解释当我没有指出我想要多少个插槽时发生了什么吗?
谢谢!
我试图理解多维数组和指针,编写这个小程序来理解这个概念:
#include<stdio.h>
void show(int arr[][2]);
int main()
{
int z[2][2] = { { 1, 2 },
{3, 4 } };
show(z);
}
void show(int arr[][2])
{
printf("value of arr = %d", arr);
printf("\n\nvalue of &arr[0]0] = %d", &arr[0][0]);
}
Run Code Online (Sandbox Code Playgroud)
这个代码片段打印相同的地址是有意义的,但是当我编辑show函数时:
void show(int arr[][2])
{
printf("value of *arr = %d", *arr);
printf("\n\nvalue of arr[0]0] = %d", arr[0][0]);
}
Run Code Online (Sandbox Code Playgroud)
*arr仍然打印相同的地址,而arr [0] [0]按预期打印整数值,我想知道为什么我需要使用**arr来获取int值,如果arr存储它应该是的地址用*arr取消引用,不是吗?
请帮助我很难理解这个概念..提前感谢.
我需要为指针打印一个名字.但它根本不起作用.控制台停止..
也许问题出在"find_young"函数上.我不知道是什么问题以及如何解决它.我能为正确的代码做些什么.
以下是我的代码.
========
typedef struct {
char *name;
int age;
} PERSON;
PERSON s[3] = { {"ACE", 25}, {"HEART" ,28}, {"CLOVER", 40} };
void find_young(PERSON **p) {
int i;
for (i = 0; i < 1; i++) {
if (s[i].age > s[i+1].age) {
p = &s[i+1];
}
}
};
void Ex1()
{
struct PERSON *p;
p = &s[0];
find_young(&p);
printf("youngest man is %s.", p->name);
}
Run Code Online (Sandbox Code Playgroud) 我有一个二维数组,我想通过一个函数
char blendModeOptions[18][16]={"Normal","Darken","Multiply","Color Burn","Linear Burn","Lighten","Screen","Color Dodge","Linear Dodge","Overlay","Soft Light","Hard Light",
"Vivid Light","Linear Light","Pin Light","Difference","Exclusion","Hue"};
blendMode = KIT_CreateSelectOption(blendModeOptions,18,&blendModeRect);
Run Code Online (Sandbox Code Playgroud)
该函数的声明为:
KIT_SelectOption * KIT_CreateSelectOption(char ** options,int size ,SDL_Rect * rect);
Run Code Online (Sandbox Code Playgroud)
我得到警告:
note: expected 'char **' but argument is of type 'char (*)[16]'
Run Code Online (Sandbox Code Playgroud)
在函数内部,我int KIT_AddOption(KIT_SelectOption *box, const char * option);通过此参数调用函数KIT_AddOption(box,options[i]);
当我在此代码行中时,我的程序崩溃了 KIT_AddOption
strcpy(opt->name,option);
Run Code Online (Sandbox Code Playgroud)
我已经尝试char * option[16]了,char ** option但没有用。
#include <stdio.h>
int a;
int main ()
{
int a, b;
int *p;
b = 8;
p = &b;
a = 32 + b;
p = &a;
*p = 32 - b;
funct (a, &p);
*p = 2;
printf ("a=%d b=%d", a, b);
}
funct (int x, int **y)
{
a = 15;
**y = x - a;
*y = &a;
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么a等于9?我试图解决它但我无法理解它
我尝试了代码code::blocks,显然a是在40到24之后
`*p=32-b`
Run Code Online (Sandbox Code Playgroud)
另外,p=&b表示该指针指向B的地址,在此之后a=32+8
p=&a和双指针*p= 32-b如此*p=24.是 …
仅双指针存储指针的地址不是真的吗?然后如何存储整数地址?
{
int **ptr,a;
a = 10;
ptr = &a;
printf("value of a = %d\n",*ptr); //why it works?
printf("value of a = %d\n",**ptr); //why it doesnt work?
}
Run Code Online (Sandbox Code Playgroud) 有人请详细说明这里有什么好玩的吗?
int main()
{
int **p = 0;
//p=? and why| *p=? and why|**p=? and why
++p;
//p=? and why| *p=? and why|**p=? and why
printf("%d\n", p);
return 1;
}
Run Code Online (Sandbox Code Playgroud)
输出: -