在 C 中,如果我们只访问分配内存中的元素,那么向数组指针分配内存不足是“合法的”吗?或者这会调用未定义的行为吗?
int (*foo)[ 10 ]; //Pointer to array of 10 ints
foo = malloc( sizeof( int ) * 5 ); //Under-allocation!
//Only enough memory for 5 ints
//Now we only ever access (*foo)[ 0 - 4 ]
Run Code Online (Sandbox Code Playgroud)
如果这本身不是未定义的行为,那么访问另一个不相关的对象(其内存地址恰好落在数组未分配部分的地址空间内)是否会导致严格别名冲突?
我想传递一个指向函数的"多态"数组.
我可以在没有警告的情况下做以下事情:
foo (void* ptr);
bar()
{
int* x;
...
foo(x);
}
Run Code Online (Sandbox Code Playgroud)
gcc显然会自动转换x为a (void*),这只是花花公子.
但是,当我执行以下操作时,我会收到警告:
foo (void** ptr);
bar()
{
int** x; // an array of pointers to int arrays
...
foo(x);
}
note: expected ‘void **’ but argument is of type ‘int **’
warning: passing argument 1 of ‘foo’ from incompatible pointer type [enabled by default]
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么一个传递(int*)的(void*)参数不是"不兼容",但(int**)作为一个(void**)论据是什么?
由于所有指针类型都是相同的大小(对吗?因为我使用了C已经有一段时间了),我仍然可以做类似的事情:
void mainFunc1(int** arr, int len)
{
//goal is to apply …Run Code Online (Sandbox Code Playgroud) 我试图从指向数组的双指针获取2D数组的行数和列数.
#include <stdio.h>
#include <stdlib.h>
void get_details(int **a)
{
int row = ??? // how get no. of rows
int column = ??? // how get no. of columns
printf("\n\n%d - %d", row,column);
}
Run Code Online (Sandbox Code Playgroud)
上面的功能需要打印大小的细节,哪里出错了.
int main(int argc, char *argv[])
{
int n = atoi(argv[1]),i,j;
int **a =(int **)malloc(n*sizeof(int *)); // using a double pointer
for(i=0;i<n;i++)
a[i] = (int *)malloc(n*sizeof(int));
printf("\nEnter %d Elements",n*n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("\nEnter Element %dx%d : ",i,j);
scanf("%d",&a[i][j]);
}
get_details(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用malloc来创建数组.
如果我使用这样的东西怎么办?
column …
我知道这int (*p)[5]意味着一个指向5个int数组的指针.所以我在下面编写这个程序:
#include <iostream>
using namespace std;
int main()
{
int a[5]={0,1,2,3,4};
int (*q)[5]=&a;
cout<<a<<endl;
cout<<q<<endl;
cout<<*q<<endl;
cout<<**q<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我的机器上,结果是:
0xbfad3608
0xbfad3608 //?__?
0xbfad3608
0
Run Code Online (Sandbox Code Playgroud)
我能理解这*q意味着地址 a[0]和**q意味着价值a[0],但为什么和?q有相同的价值?我心情不好,应该是他们的地址!我完全糊涂了.有人请帮帮我.请!a*q
我有这个代码:
#include <stdio.h>
int main()
{
int arr[10] = {0};
int *p1_arr = arr;
int (*p2_arr)[10] = arr; // Line 7, Shows Warning here
...
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在编译gcc使用时gcc -g -Wall LengthofArray.c,它显示以下警告:
gcc: LengthOfArray.c:7: [Warning] assignment from incompatible
pointer type [enabled by default]
Run Code Online (Sandbox Code Playgroud)
我的问题是if int (*p2_arr)[10]是一个指向大小为10的数组的指针,那么为什么编译器显示这个警告?
那么正确的方法是什么呢?
我在Windows 7 32位(DevC++)上使用了gcc 4.7.2,
并在SLES 10.3 x86_64上检查了gcc 4.1.2
#include <stdio.h>
int main()
{
int (*ptr) [5];
printf ("%lu\n" , sizeof (ptr));
printf ("%lu\n" , sizeof (*ptr));
}
Run Code Online (Sandbox Code Playgroud)
我使用的是64位系统。当我在 gcc 编译器中运行此代码时,它会生成输出8 and 20。我的问题是为此分配多少字节pointer to an array?是 8 或 20,为什么?
我有2-D阵列
char arr[2][3]={"sam","ali"}
Run Code Online (Sandbox Code Playgroud)
和指向此数组的指针
char(*ptr)[3]=arr;
Run Code Online (Sandbox Code Playgroud)
如何使用此指针打印arr[2][2]在这种情况下i.我已经尝试* (*(ptr+1)+2)过与处理数组相同的方式,但没有工作,所以可以任何一个帮助并告诉我如何处理指向数组的指针,在这种情况下打印元素[2][2].