相关疑难解决方法(0)

为什么函数不知道数组大小?

如果我写

int main()
{
    int a[100] = {1,2,3,4,};
    cout<<sizeof(a)/sizeof(a[0])<<endl; //a is a pointer to the first elem of array, 
                                        //isn't it
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到400!

如果我写

void func(int*a);

int main()
{
    int a[100] = {1,2,3,4,};
    func(a);
    return 0;
}

void func(int *a)
{
     cout<<sizeof(a)/sizeof(a[0])<<endl; //a is a pointer to the first elem of array
}
Run Code Online (Sandbox Code Playgroud)

然后我得到1!

那么为什么函数不知道数组大小呢?

c++ pointers function sizeof

1
推荐指数
1
解决办法
344
查看次数

在运行时以标准C(不在C99中)声明数组的大小

数组需要在编译时定义大小.有没有可能在运行时使用malloc或者其他什么来定义数组的大小?

c malloc dynamic

1
推荐指数
1
解决办法
1136
查看次数

c ++使用ptr作为数组基础

在Effective C++的第42页上,指针用作数组名称ala

AirPlane*newBlock = ...

newBlock [I]的.next = 0;

我没有意识到这是合法的.这是c ++标准的一部分吗?这是常见做法吗?

c++ arrays pointers

0
推荐指数
1
解决办法
185
查看次数

数组简介

可能重复:
我们何时需要将数组大小作为参数传递

所以我刚开始使用数组,我需要创建3个函数来让我学习.

int sumarray(int a[], int n);

// a is an array of n elements
// sumarray must return the sum of the elements
// you may assume the result is in the range
//    [-2^-31, 2^31-1]

int maxarraypos(int a[], int n);

// a is an array of n elements
// maxarraypos must return the position of
//   the first occurrence of the maximum
//   value in a
// if there is no such value, must return 0

bool …
Run Code Online (Sandbox Code Playgroud)

c arrays

0
推荐指数
1
解决办法
470
查看次数

C编程:数组和指针

可能重复:
数组名是C中的指针吗?

如果我定义:

int tab[4];
Run Code Online (Sandbox Code Playgroud)

tab是一个指针,因为如果我显示选项卡:

printf("%d", tab);
Run Code Online (Sandbox Code Playgroud)

上面的代码将显示内存中第一个元素的地址.

这就是为什么我想知道为什么我们不定义如下的数组:

int *tab[4];
Run Code Online (Sandbox Code Playgroud)

因为tab是一个指针.

感谢您的任何帮助!

c pointers

0
推荐指数
1
解决办法
435
查看次数

为什么动态内存超出范围时不会被清除

我刚开始玩C++(熟悉其他语言),我想知道一些事情.

在创建动态大小的数组时,例如使用new时,为什么它在超出范围时不会被删除,但是具有固定大小的数组会被删除?

例如,使用以下代码:

int foo()
{
    int baz[5]; // Gets removed out of scope
    int *bar = new int[5]; // Does not get removed, becomes a leak
}
Run Code Online (Sandbox Code Playgroud)

由于baz和bar都是指向数组开头的指针,因此"运行时环境"不能应用相同的技术(检测是否超出范围,并将其删除时)应用于具有固定大小的事物动态大小?

它不能做这样的事情有什么不同.

c++ memory-leaks memory-management

0
推荐指数
1
解决办法
154
查看次数

新数据类型*[10]如何返回指针指针?

我在C++中遇到了Hash Map实现.HashMap的构造函数包含下面的代码.这条线new HashEntry*[TABLE_SIZE]说的是什么.我以前从未见过这样的结构.它如何返回指针指针?

class HashMap {
    private:
          HashEntry **table;
    public:
          HashMap() {
                table = new HashEntry*[TABLE_SIZE];
                for (int i = 0; i < TABLE_SIZE; i++)
                      table[i] = NULL;
          }
};
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers new-operator

0
推荐指数
1
解决办法
78
查看次数

指向内存的指针

我正在学习C并且有一些问题.我正在试图为每个声明的变量打印内存插槽,但是当我为Char []声明指针时,它只是不起作用.

这是我的代码:

int main () {
    char a[3]; // this variable is my problem
    int b;
    float c;
    char d;
    int e=4;

    char *pachar; //A char type variable for the pointer.
    int *paint;
    float *pafloat;
    char *pacharr;
    int *paintt;

    pachar = &a; // when I try to assign the memory to the pointer, it shows a Warning message.
    paint = &b;
    pafloat = &c;
    pacharr = &d;
    paintt = &e;


    printf("%p \n",pachar);
    printf("%p \n",paint);
    printf("%p \n",pafloat);
    printf("%p \n",pacharr);
    printf("%p …
Run Code Online (Sandbox Code Playgroud)

c pointers char

0
推荐指数
1
解决办法
141
查看次数

"数组"元素值是存储在一个位置还是存储在不同的位置?

由于C语言中没有数组这样的东西,以下是否都存储在一个内存位置,或者每个元素的值存储在内存位置的"数组"中?

int array[] = {11, 13, 17, 19};
Run Code Online (Sandbox Code Playgroud)

哪一个是有效的内存布局?

c arrays pointers memory-layout

0
推荐指数
1
解决办法
2210
查看次数

为什么在这种情况下 void** 会导致段错误?

我很困惑为什么这段代码会导致段错误

int array[] = {1, 2};
void** ptr = (void**) &array;
printf("%d", *(int*)ptr[0]);
Run Code Online (Sandbox Code Playgroud)

我希望它打印存储在 array[0] 中的 int,但它却出现段错误。

c

0
推荐指数
1
解决办法
117
查看次数