标签: pointers

C中的数组是通过指针使用的吗?

我在这里发布的一个问题:C++ - 类问题

其中一封来自@SanSS的回复提到了以下部分回复:

C中的数组通过指针使用...

这是怎么做到的?并且,如果可能的话,你能通过一个例子澄清这一点吗?

谢谢.

c arrays pointers

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

+ =运算符链接(带有少量UB)

我知道在分号之前没有序列点,但对于在表达式中使用旧值2的解引用指针是否有合理的解释?

或者它可以简单地作为未定义的行为放下?

int i=2;
int *x=&i;
*x+=*x+=i+=7;
Run Code Online (Sandbox Code Playgroud)

结果:

i= 13
Run Code Online (Sandbox Code Playgroud)

c pointers chaining undefined-behavior

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

这可以将NSDate变量赋值为零吗?

假设我想要删除存储在NSString变量中的值,我将为其分配一个值nil.这个有可能.但是,如果我想删除已经包含某些值的日期变量,我该怎么办?

iphone pointers objective-c nsdate

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

C中的组合解除引用和减少

我需要尽可能高效的方法来移动数组的内容.我需要将每个数组位置的内容向右移动并忽略第一个,这样我就可以在那里写一个新值.

这就是我所拥有的:

#define LENGTH 5

int myArray[LENGTH] = {1, 2, 3, 4, 5};

int *pa = myArray + (LENGTH - 1);

for (ushort i = 5; i > 0; i--) {
    *pa = *(pa - 1);
    pa--;
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是将for循环的两行组合成一个操作.就像是:

*pa = *(pa--);
Run Code Online (Sandbox Code Playgroud)

但是,结果是未定义的.我坚持使用我已经使用的东西吗?

编辑:我应该澄清这不是我正在使用的实际代码,只是一个快速的例子来演示我所追求的构造.

c performance pointers dereference decrement

0
推荐指数
2
解决办法
718
查看次数

在C++中有效表示两个堆栈?

假设某些应用程序需要使用两个元素属于同一类型的堆栈.这种双栈数据类型的自然存储结构将由两个阵列和两个顶部指针组成.解释为什么这可能不是一个空间有效的实现.

c++ queue stack pointers data-structures

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

为什么不*(ptr ++)给出数组中的下一个项目?

int my_array[] = {1,23,17,4,-5,100};
int *ptr;
int i;
ptr = &my_array[0];     /* point our pointer to the first
                         element of the array */
printf("\n\nptr = %d\n\n", *ptr);
for (i = 0; i < 6; i++)
{
    printf("my_array[%d] = %d   ",i,my_array[i]);   /*<-- A */
    printf("my_array[%d] = %d\n",i, *(ptr++));        /*<-- B */
}
Run Code Online (Sandbox Code Playgroud)

为什么这对a和b线都显示相同的东西?它只是按顺序显示my_array中的所有值(1,23,17,4,-5,100).为什么B行中的'++'在取消引用之前没有将ptr指向数组的下一个元素?即使您将该行更改为

printf("ptr + %d = %d\n",i, *ptr++);        /*<-- B */
Run Code Online (Sandbox Code Playgroud)

输出是一样的.为什么是这样?

c arrays pointers

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

调试指针算术

当我打印变量地址之间的差异时,我无法解决为什么我找到差异

这是代码:

int main()
{

        int *a,b = 5,e = 0;
        int *c,d = 10,f = 0;
        long t1,t2;
        a = &b;
        c = &d;
        e = &b;
        f = &d;
        t1 = e - f;
        t2 = a - c;
       printf("\n Address of b using a: %x \t %d using e : %x \t %d value of b : %d",a,a,e,e,b);
       printf("\n Address of d using c: %x \t %d using f : %x \t %d value of d : …
Run Code Online (Sandbox Code Playgroud)

c pointers

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

将结构作为指针传递,导致数组损坏?

我有一个结构作为void*指针传入

void *find_queens(void *args) {  
Run Code Online (Sandbox Code Playgroud)

我尝试使用此方法将此指针转换为可用的结构

struct queens_arg *data = (struct queens_arg *) args;
Run Code Online (Sandbox Code Playgroud)

但是,存储在此内的数组

struct queens_arg {
  int board[64]; 
  int focus_idx;
};
Run Code Online (Sandbox Code Playgroud)

被称为董事会现在正在被破坏,并没有反映原始价值,有谁知道为什么?谢谢!

更多信息:

这是函数的开始:

void *find_queens(void *args) {  

  //vars
  pthread_t thread1, thread2;
  struct queens_arg *data = (struct queens_arg *) args;
  int board[64];
  copy_array(data->board, board);
  print_board(data->board);
Run Code Online (Sandbox Code Playgroud)

这就是它的名称:

int board[64] = {
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
  };

  struct queens_arg *args = malloc(sizeof (struct queens_arg));
  args->focus_idx = 0;
  copy_array(board,args->board);
  (*find_queens)(&args);
Run Code Online (Sandbox Code Playgroud)

当我打印数组时,我得到了这个:

39456784 0 0 0 0 0 …
Run Code Online (Sandbox Code Playgroud)

c struct pointers

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

如何创建一个随机混乱字符串的函数

我的main函数包含一个我想混杂的字符串.我找到了一段传入a的代码,char*完成后返回0.我已经按照代码提供程序的说明简单地传入了你想要混乱的字符串.

#include <iostream>
#include <string.h>
#include <time.h>
using namespace std;

int scrambleString(char* str)
{
     int x = strlen(str);
     srand(time(NULL));
     for(int y = x; y >=0; y--)
     {
          swap(str[rand()%x],str[y-1]);
     }
     return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我收到一个错误"no suitable conversion function "std::string" to "char*" exists.

我也试过传入const char*但是不允许我访问这个词并改变它.

c++ string pointers char

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

什么是使用QT库的c ++中这行代码的等价物?

鉴于声明

   class DBuffer
{
//...
};

typedef QList<DBuffer*> DBuffers;
QList<int> fds;
QMap<int, DBuffers> buffers; 
Run Code Online (Sandbox Code Playgroud)

下面给出的函数中的代码行是什么意思.

function()
{
 // what does this line mean? what is "&bufs"

    DBuffers &bufs=buffers[fds[i]];
}
Run Code Online (Sandbox Code Playgroud)

c++ qt pointers qt4 qlist

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