main()
{
int arr[] = {1, 2, 3, 4};
function(arr);
}
int function(int a[])
{
}
Run Code Online (Sandbox Code Playgroud)
在这里,我想获得未初始化的数组的长度.在我的情况下的主要功能是我不知道..假设我从其他程序获取数组元素.
可能重复:
如何查找sizeof(指向数组的指针)
我有以下代码
int myfunc(char *arr)
{
const int sizearr = sizeof( arr ) / sizeof(char);
}
Run Code Online (Sandbox Code Playgroud)
这会产生指针的大小,而不是我传递给函数的char数组[17]的大小.
我知道我可以使用strlen和解决该问题,但没有任何方式对字符*ARR指针转换回char数组[17]我传递给函数,以使用的sizeof(char数组[17]) ?
可能重复:
如何查找sizeof(指向数组的指针)
我有以下程序计算数组中位置之间的最大差异,但sizeof函数不打印出预期的.当通过10个大的数组并且用尺寸测量它时它打印出它是8英尺长,怎么样?
#include <stdlib.h>
#include <stdio.h>
int *
measure(int *arr)
{
int *answer=malloc(sizeof(int)*10);
answer[0]=0;
answer[1]=0;
int size=sizeof(arr)+1;
printf("%d\n", size);
int i;
for(i=0; i<size; i++)
{
signed int dif=arr[i]-arr[i+1];
if(dif<0)
dif*=-1;
if(dif>answer[1])
{
answer[1]=dif;
answer[0]=i;
}
}
return answer;
}
int
main (void)
{
int arr[10]={77, 3, 89, 198, 47, 62, 308, 709, 109, 932};
int *ans=measure(arr);
printf("the biggest dif is %d between nrs %d and %d\n", ans[1], ans[0], ans[0]+1);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在C中有以下代码:
int array[5] = {0,1,2,3,4};
int * p = &array[0];
Run Code Online (Sandbox Code Playgroud)
如何使用指针p获取此数组的大小p指向?
我运行以下代码,但它一直打印"4"
为什么它的打印"4"而不是"12"?我可以使用malloc然后使用sizeof吗?(如果可以,那么如何)
#include<stdio.h>
int main()
{
int arr1[3]={1,2,3};
int *arr2=arr1,i;
printf("%d",sizeof(arr2));
return 0;
}
Run Code Online (Sandbox Code Playgroud) typedef struct {
double *x;
int points;
} Polygon;
polygon[cc].points = readobject(polygon[cc].file); //lets say equals 15
polygon[cc].x = (double *) malloc(polygon[cc].points * sizeof(double));
printf("%d\n", sizeof(polygon[cc].x) / sizeof(polygon[cc].x[0]));
//This prints out on 1
Run Code Online (Sandbox Code Playgroud)
我测试初始化x为x[100],然后它工作,但不应该malloc()调用设置结构的数组x[15]?它只是测量两个doubles 的字节吗?
char *foo(char *dest, const char *src) {
size_t i;
for (i = 0; dest[i] != '\0'; i++);
Run Code Online (Sandbox Code Playgroud)
在这里,我正在迭代以获得dest的大小.在这种情况下,我将"hello"输入到dest中,其大小为6.当我尝试使用sizeof(dest)时,我得到4作为返回值.我希望能够在不使用for循环的情况下获取dest内部的内容大小.
char *foo(char *dest, const char *src) {
while (*dest != '\0') dest++; /* increment the length of dest's pointer*/
Run Code Online (Sandbox Code Playgroud)
编辑::我想花点时间表明我能够直接找到长度.
这是strcat程序的一部分.要求是不要使用[]括号来访问或在内存中移动.
char *strcat(char *dest, const char *src) {
while (*dest != '\0') dest++; /* increment the length of dest's pointer*/
while (*src != '\0') /* we will be incrementing up through src*/
*dest++ = *src++; /* while this is happening …Run Code Online (Sandbox Code Playgroud) free()只需要指针值来释放已分配的内存.这意味着C知道分配的内存块有多大.那么为什么没有一些内置函数来查找指针数组的大小?
我知道惯例是跟踪数组大小,但鉴于已经有一些本地发生的内存管理,为什么我们不利用它来size()为数组提供方便的功能?
我有一个十六进制值的字符指针,如下所示.a = {0x01,0x02,0x00,0x03,0x04,0x05,0x00}; 我必须找到上面数组的长度.但是strlen给出2并且sizeof给出4(char指针大小).我怎样才能找到长度?我从套接字接收字符数组.
在下面的代码中,我初始化了一个 int 指针,然后动态分配了内存。
但是当我想显示数组的大小时,它不会显示值5:
int *p=NULL;
int tab[]={};
p=tab;
p=malloc(5*sizeof(int));
printf("sizeof(tab)=%d",(int)sizeof(tab));
Run Code Online (Sandbox Code Playgroud)