据我所知,const int *暗示我可以改变指针而不是数据,int * const说我不能改变指针地址,但我可以改变数据,并const int * const声明我不能改变它们中的任何一个.
但是,我无法更改使用该类型定义的指针的地址const int *.这是我的示例代码:
void Func(const int * pInt)
{
static int Int = 0;
pInt = ∬
Int++;
}
int wmain(int argc, wchar_t *argv[])
{
int Dummy = 0;
const int * pInt = &Dummy;
//const int * pInt = nullptr; // Gives error when I try to pass it to Func().
std::cout << pInt << '\t' << *pInt << std::endl;
std::cout << "-------------------" …Run Code Online (Sandbox Code Playgroud) 这句话来自SICP,我认为是在讨论编程语言中的指针/引用.
正如我们所看到的,对提供了一个原始的"粘合剂",我们可以用它来构造复合数据对象.图2.2显示了可视化一对的标准方法 - 在这种情况下,由(cons 1 2)形成的对.在这种称为盒子和指针表示法的表示中,每个对象都显示为指向盒子的指针.原始对象的框包含对象的表示.例如,数字框包含数字.一对的盒子实际上是一个双盒子,左边的部分包含(指向)该对的汽车,右边的部分包含cdr.
在这种情况下,这本书正在讨论对(EG(缺点1 2))以及如何表示它们.但是我们也可以使用pair来构造这样的列表:
(缺点1(缺点2'()))
虽然box-and-pointer-notation只是一种符号而且毫无用处,但我认为这看起来很像一个链表.据我了解,链表是一个数据结构,包含一个值和指向另一个链表的指针.话虽如此,我认为缺点可以像链表一样构建.我很困惑:
一对的盒子实际上是一个双盒子,左边的部分包含(指向)该对的汽车,右边的部分包含cdr.
我原本以为指针应该在cdr上,因为如果我们通过对构建一个列表,那将是下一个列表.
我认为这可能是一种不同的指针.在这种情况下,指针到底意味着什么?我知道的唯一指针是c中使用的指针.SICP甚至提到了有关c指针的任何内容吗?
使用valgrind读取此消息,我得到:大小为4的无效写入/读取
struct Person{
char* name;
int age;
};
struct Person* create_person(char *name, int age)
{
struct Person* me = (struct Person*)malloc(sizeof(struct Person*));
assert(me!= NULL); //make sure that the statement is not null
me->name = name;
me->age = age;
return me;
}
Run Code Online (Sandbox Code Playgroud)
使用此得到的干净日志与valgrind
struct Person{
char* name;
int age;
};
struct Person* create_person(char *name, int age)
{
struct Person* me = (struct Person*)malloc(sizeof(struct Person*)+4);
assert(me!= NULL); //make sure that the statement is not null
me->name = name;
me->age = age;
return …Run Code Online (Sandbox Code Playgroud) 我正在使用VS 2013和c开发一个现有项目.
我来到这个功能,但我不明白这意味着什么:
int (*Read)(void *p, void *buf, size_t *size);
Run Code Online (Sandbox Code Playgroud)
我所知道的是它是一个返回指向int的指针的函数,但我想知道:
- 为什么void as type意味着在处理参数时(可能是通用指针?)
- 什么(*读)意味着在这种情况下(我认为这是一个演员!)
真正知道我在这里使用的是完整的依赖项:
typedef struct
{
#ifdef USE_WINDOWS_FILE
HANDLE handle;
#else
FILE *file;
#endif
} CSzFile;
typedef struct
{
int (*Read)(void *p, void *buf, size_t *size);
} ISeqInStream;
typedef struct
{
ISeqInStream s;
CSzFile file;
} CFileSeqInStream;
CFileSeqInStream inStream;
Run Code Online (Sandbox Code Playgroud)
最后的目标是打开一个文件,但为什么所有这些并发症!
任何帮助将不胜感激,谢谢.
我对这段代码的输出感到困惑:
#include <stdio.h>
#include <stdlib.h>
#define TIMING_OUTPUT_FILENAME_MAX_LENGTH 40
int main (int argc, char **argv)
{
char *timing_output_filename = malloc(TIMING_OUTPUT_FILENAME_MAX_LENGTH);
printf("requested buffer size is %ld bytes and pointer size is %ld bytes\n",
sizeof(timing_output_filename),
sizeof(*timing_output_filename));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
requested buffer size is 8 bytes and pointer size is 1 bytes
Run Code Online (Sandbox Code Playgroud)
它应该给我40个字节?我错过了什么吗?
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) 我需要返回对我写的Class的私有成员的引用.我是这样做的:
在MyClass2.h中,我有以下行
MyClass* getObj(){return &instance_myclass;}
Run Code Online (Sandbox Code Playgroud)
我将确保我的代码不会更改instance_myclass的任何值.但只是为了确保,是否有一种方法可以返回此参考只读,以便我无法更改其值?或者,根据定义,这是不可能的?
我跑的时候
char * a = "string";
char * b = a;
while (*a != '\0')
printf("%p %c\n", *(a), *(a++));
printf("%p\n", *(b+2));
Run Code Online (Sandbox Code Playgroud)
输出看起来像
0x73 s
0x74 t
0x72 r
0x69 i
0x6e n
0x67 g
0x72
Run Code Online (Sandbox Code Playgroud)
此外,程序如何确定(b + 2)位于0x72.我认为它只会将2添加到b的起始地址,在本例中为0x73.
编辑:这不是未指定行为的情况.正如答案中所解释的那样,我错误地将值传递给地址格式说明符而不是指针本身.
所以我一直在忙着编写自己的自定义内存分配器,但是我遇到了一些我不理解的奇怪行为.
考虑以下代码:
void* PointerUtil::AlignForward(void* address, const uint8_t& alignment)
{
return (void*)(((uintptr_t)(address) + (uintptr_t)(alignment - 1)) & (uintptr_t)(~(alignment - 1)));
}
Run Code Online (Sandbox Code Playgroud)
这应该采用指针和对齐要求,并修改指针使其在正向(正向)上正确对齐.
但是,当我这样测试时:
int address = 1240;
std::cout << (uintptr_t)memory::PointerUtil::AlignForward((void*)((uintptr_t)address), 512) << std::endl;
std::cout << (uintptr_t)memory::PointerUtil::AlignForward((void*)((uintptr_t)address), 256) << std::endl;
std::cout << (uintptr_t)memory::PointerUtil::AlignForward((void*)((uintptr_t)address), 128) << std::endl;
std::cout << (uintptr_t)memory::PointerUtil::AlignForward((void*)((uintptr_t)address), 64) << std::endl;
std::cout << (uintptr_t)memory::PointerUtil::AlignForward((void*)((uintptr_t)address), 32) << std::endl;
Run Code Online (Sandbox Code Playgroud)
我得到的输出是这样的:
0
0
1280
1280
1248
Run Code Online (Sandbox Code Playgroud)
这似乎不对.它应该是:
1536
1280
1280
1280
1248
Run Code Online (Sandbox Code Playgroud)
这里出了什么问题?
我在这个链接中看到了这段代码 - http://www.tutorialspoint.com/cplusplus/cpp_pointers_vs_arrays.htm.看看第一段代码.
#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr;
// let us have array address in pointer.
ptr = var;
for (int i = 0; i < MAX; i++)
{
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
// …Run Code Online (Sandbox Code Playgroud)