相关疑难解决方法(0)

C中空指针的指针算法

当一个指针到特定类型的(比如int,char,float,..)被递增,其值增加该数据类型的大小.如果void指向大小数据的指针x递增,它如何获得指向x前面的字节?编译器如何知道添加x指针的值?

c pointers void-pointers pointer-arithmetic

162
推荐指数
7
解决办法
11万
查看次数

C中的指针算法

我有以下代码.也许我没有理解指针算法以及我应该有但是为什么int_pointer增加4而不是1?使用char_pointer,为什么它不会增加4而不是1?

 #include <stdio.h>

 int main() {
    int i;

    char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
    int int_array[5] = {1, 2, 3, 4, 5};

    char *char_pointer;
    int *int_pointer;

    char_pointer = int_array; // The char_pointer and int_pointer now
    int_pointer = char_array; // point to incompatible data types.

    for(i=0; i < 5; i++) { // Iterate through the int array with the int_pointer.
        printf("[integer pointer] points to %p, which contains the char '%c'\n",
            int_pointer, *int_pointer);
        int_pointer = int_pointer + 1;
    }

    for(i=0; …
Run Code Online (Sandbox Code Playgroud)

c pointers

11
推荐指数
2
解决办法
4470
查看次数

表达式必须是使用简单指针算法指向完整对象类型的指针

我正在尝试用void*做一些基本的指针算法.我的实际代码通过使用sizeof然后乘法来计算偏移量.下面是示例代码,用于显示问题的实例.

void * p;
p = 0;
p = p + 1;
Run Code Online (Sandbox Code Playgroud)

我在C(而不是C++)中使用MSVC编译器.

错误是:

expression must be a pointer to a complete object type  
Run Code Online (Sandbox Code Playgroud)

我不明白这个错误试图说的是什么.这里没有对象或结构.

c

10
推荐指数
1
解决办法
3万
查看次数

指针的类型

我是C++的新手,因为我正在阅读麻省理工学院关于指针的讲义,我认识到一些奇怪的东西:

指针只是存储整数的变量 - 但这些整数恰好是内存地址,通常是其他变量的地址.存储某个变量x的地址的指针被称为指向x.我们可以通过解除引用指针来访问x的值.

以及我发现指针可以有一个类型:

int *pointer ; 
char * pointer ; //example 
Run Code Online (Sandbox Code Playgroud)

好吧,它只是说它是一个int,它持有一个地址为什么给它与它指向的东西相同的类型,如果它只是持有对它的引用而不是实际值?

c++ pointers

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

标签 统计

c ×3

pointers ×3

c++ ×1

pointer-arithmetic ×1

void-pointers ×1