标签: pointers

C++ singleton GetInstance()返回

在C++中实现单例时,GetInstance()返回指向单例对象或引用的指针是否更好?真的有关系吗?

c++ singleton pointers reference

15
推荐指数
2
解决办法
9590
查看次数

当应用于变量名时,*和&的含义是什么?

在C++中,有什么区别:

 void func(MyType&); // declaration

 //...
 MyType * ptr;

 func(*ptr); // compiler doesnt give error

 func(ptr); // compiler gives error i thought & represents memory address so 
            // this statement should correct as ptr is only a pointer
            // or address of some real var.
Run Code Online (Sandbox Code Playgroud)

c++ pointers reference c++-faq

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

typedef的指针类型被认为是不好的做法吗?

可能重复:
Typedef指针是个好主意?

我在许多使用过的API中都看到了这种奇怪之处:

typedef type_t *TYPE;
Run Code Online (Sandbox Code Playgroud)

我的观点是声明一个类型的变量TYPE将不会清楚地表明事实上已经声明了一个指针.

你和我一样认为这会带来很多困惑吗?这是否意味着强制执行封装,还是有其他原因?你认为这是一种不好的做法吗?

c pointers typedef

15
推荐指数
4
解决办法
5369
查看次数

*[]和**之间的C差异

这可能是一个基本问题,但写char*[]和char**有什么区别?例如,在main中,我可以有一个char*argv [].或者我可以使用char**argv.我假设两种符号之间必须存在某种差异.

c arrays pointers

15
推荐指数
3
解决办法
2479
查看次数

功能是如何计算的?

我理解currying的概念是什么,并且知道如何使用它.这些不是我的问题,而是我很好奇这个实际上是如何在比Haskell代码更低的层次上实现的.

例如,当(+) 2 4curried时,是一个指向2维持的指针,直到4传入?甘道夫是否会缩短时空?这个魔法是什么?

haskell pointers functional-programming currying low-level

15
推荐指数
2
解决办法
312
查看次数

方括号数组和指针数组有什么区别?

作为非C/C++专家,我总是将方括号和指针数组视为相等.

即:

char *my_array_star;
char my_array_square[];
Run Code Online (Sandbox Code Playgroud)

但是我注意到在结构/类中使用时它们的行为并不相同:

typedef struct {
   char whatever;
   char *my_array_star;
} my_struct_star;

typedef struct {
   char whatever;
   char my_array_square[];
} my_struct_square;
Run Code Online (Sandbox Code Playgroud)

下面的行显示16,whatever占用1个字节,my_array_pointer占用8个字节.由于填料,总结构尺寸为16.

printf("my_struct_star: %li\n",sizeof(my_struct_star));
Run Code Online (Sandbox Code Playgroud)

下面的行显示1,whatever占用1个字节,my_array_pointer不考虑帐户.

printf("my_struct_square: %li\n",sizeof(my_struct_square));
Run Code Online (Sandbox Code Playgroud)

通过玩耍,我注意到方括号被用作结构中的额外空间

my_struct_square  *i=malloc(2);

i->whatever='A';
i->my_array_square[0]='B';
Run Code Online (Sandbox Code Playgroud)

线吹显示A:

printf("i[0]=%c\n",((char*)i)[0]);
Run Code Online (Sandbox Code Playgroud)

线吹显示B:

printf("i[1]=%c\n",((char*)i)[1]);
Run Code Online (Sandbox Code Playgroud)

所以我不能再说方括号等于指针了.但我想了解这种行为的原因.我害怕错过这些语言的关键概念.

c c++ arrays pointers

15
推荐指数
2
解决办法
8499
查看次数

如何用C++中的引用替换指针?

"我确信有几十个问题都有相同的标题.其中很多都是重复的.我的也可能是重复的,但我找不到任何问题.所以我试着让它变得非常简洁,简洁."

我有这样的层次结构:

class Shape {
public:
   virtual void virtualfunc()  { std::cout << "In shape\n"; }
};

class Circle: public Shape {
public:
   void virtualfunc()  { std::cout << "In Circle\n"; };
};
Run Code Online (Sandbox Code Playgroud)

当我在指针的帮助下使用类时,函数按照我的预期调用:

int main() {
   Shape shape_instance;
   Shape* ref_shape = &shape_instance ;
   Circle circle_instance;
   Circle* ref_circle = &circle_instance;

   ref_shape = dynamic_cast<Shape*> (ref_circle);
   ref_shape->virtualfunc();
}
Run Code Online (Sandbox Code Playgroud)

这里程序调用virtualfunc()派生类,结果自然是:In Circle

现在,我想摆脱指针,改为使用引用,并获得相同的结果.所以我做了一些微不足道的修改main(),看起来像这样:

int main() {
   Shape shape_instance;
   Shape& ref_shape = shape_instance;
   Circle circle_instance;
   Circle& ref_circle = circle_instance;

   ref_shape = dynamic_cast<Shape&>(ref_circle); …
Run Code Online (Sandbox Code Playgroud)

c++ pointers reference

15
推荐指数
2
解决办法
1587
查看次数

减少指针超出范围; 将它增加到边界

以下是否会在第4行和/或第5行中引发未定义的行为:

#include <stdio.h>
int main(void)
{
  char s[] = "foo";
  char * p = s - 1;      /* line 4 */
  printf("%s\n", p + 1); /* line 5 */
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c pointers undefined-behavior

15
推荐指数
3
解决办法
1213
查看次数

如何使用指针复制字符串

这是我写的一个复制字符串常量的程序.

程序运行时崩溃.为什么会这样?

#include <stdio.h>

char *alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char c;
char *l;

main(){
   while((c = *alpha++)!='\0')
       *l++ = *alpha;
   printf("%s\n",l);
}
Run Code Online (Sandbox Code Playgroud)

c string pointers

15
推荐指数
2
解决办法
7万
查看次数

我在C中的(char*)元素数组上有三个循环.为什么第三个失败?

在尝试使用C语言中的字符串数组步进的方法时,我开发了以下小程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef char* string;

int main() {
  char *family1[4] = {"father", "mother", "son", NULL};
  string family2[4] = {"father", "mother", "son", NULL};

  /* Loop #1: Using a simple pointer to step through "family1". */
  for (char **p = family1; *p != NULL; p++) {
    printf("%s\n", *p);
  }
  putchar('\n');

  /* Loop #2: Using the typedef for clarity and stepping through
   * family2. */
  for (string *s = family2; *s != NULL; s++) {
    printf("%s\n", *s);
  } …
Run Code Online (Sandbox Code Playgroud)

c arrays pointers memory-address

15
推荐指数
2
解决办法
1304
查看次数