标签: pointers

排序结构qsort的指针数组

我正在尝试对指向结构的指针数组进行排序,其中要比较的键是结构的属性之一.

我想这可能是比较方法.

这是一个示例代码.

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

struct BINARY_ARRAY_RECORD {
    char *name;
};

int compare(const void *node1, const void *node2) {
    return strcmp(
        ((struct BINARY_ARRAY_RECORD *) node1)->name,
        ((struct BINARY_ARRAY_RECORD *) node2)->name
    );
}

int main()
{
    struct BINARY_ARRAY_RECORD **records;

    records = malloc(sizeof(struct BINARY_ARRAY_RECORD *) * 2);

    records[0] = malloc(sizeof(struct BINARY_ARRAY_RECORD));
    records[1] = malloc(sizeof(struct BINARY_ARRAY_RECORD));

    records[0]->name = malloc(sizeof(char) * (strlen("string2") + 1));
    records[1]->name = malloc(sizeof(char) * (strlen("string1") + 1));

    strcpy(records[0]->name, "string2");
    strcpy(records[1]->name, "string1");

    qsort(records, 2, sizeof(records[0]), compare);

    printf("%s\n", records[0]->name); …
Run Code Online (Sandbox Code Playgroud)

c pointers qsort

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

为什么指针在64位和32位系统中保持相同的大小?

我最近在我的计算机上安装了64位操作系统,我认为这样sizeof(char*)会给我8而不是4.我不应该在指针中获得64位地址吗?

pointers 32bit-64bit

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

两个昏暗的数组,指向它的一部分和堆栈损坏的指针

为什么下一个代码给我变量x周围的堆栈已损坏

char x[1][21];
char *ch = x[1];
strcpy(ch,"12345678901234567890");

for (int i = 0; i < 20; i++)
    cout << i << ": " << &x[1][i] << " " << x[1][i] << endl;
Run Code Online (Sandbox Code Playgroud)

输出:

0: 12345678901234567890 1
1: 2345678901234567890 2
...
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers

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

反转链表

任务是反转一个链表,所以我建立链表,然后打印出来,然后所有反向功能,然后我们第二次打印它.但是,第二次打印是空的.我认为这是指针问题,任何人都可以解释一下?谢谢.

void reverseLinkedList(struct node** head) {
    struct node* curr, *prev, *temp;

    curr = *head;
    prev = NULL;

    while (curr) {
        temp = prev;
        prev = curr;        
        curr = curr->next;
        prev = temp;
    }
    *head = prev;
}

struct node* buildLinkedList(int list[], int len) {
    struct node* head = NULL;
    struct node* tail = NULL;
    struct node* node;
    int i;

    for (i = 0; i < len; i++) {
        node = (struct node*) malloc(sizeof(struct node));
        node->data = list[i];
        node->next = NULL; …
Run Code Online (Sandbox Code Playgroud)

c printing reverse pointers linked-list

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

将堆分配的引用分配给堆栈分配的变量

这可能吗?根据我想要完成的事情,似乎并非如此.

功能

static std::string str_repeat(std::string * str, int num_times) {

    std::string * str_rep = new std::string;

    for (int n = 1; n <= num_times; n++) {
        str_rep = str_rep + str;
    }

    std::string ret = *str_rep; //error

    delete str;
    delete str_rep;

    return ret;
}
Run Code Online (Sandbox Code Playgroud)

更新

对不起,我没有首先发布错误,因为我认为这是一个普遍的C++问题,我做错了.这里是.

error: invalid operands of types ‘std::string* {aka std::basic_string<char>*}’ and ‘std::string* {aka std::basic_string<char>*}’ to binary ‘operator+’
Run Code Online (Sandbox Code Playgroud)

c++ memory heap stack pointers

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

理解C malloc和sbrk()

我试图理解C中malloc和sbrk之间的区别以及它们之间的关系.据我所知,malloc和sbrk几乎是一回事,但我读到malloc在分配内存时使用了sbrk.如果有人向我解释,这真的令人困惑吗?

例如在这个程序中malloc调用sbrk吗?如果是这样的话,它每次调用时都会调用sbrk,所以对于这个例子10次?

int main(int argc, char **argv) {
        int i;
        void *start_pos, *finish_pos;
        void *res[10];
        start_pos = sbrk(0);
        for (i = 0; i < 10; i++) {
                res[i] = malloc(10);
        }
        finish_pos = sbrk(0);
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

谢谢,

c malloc pointers

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

重载运算符()

我有这个声明

struct Z {
    void operator ()( int a ) {
        cout << "operator()() " << a << endl;
    }
};

Z oz, *zp = &oz;

oz(1); //ok
(*zp)(2); //ok
zp(3); //"error: 'zp' cannot be used as a function"
Run Code Online (Sandbox Code Playgroud)

有没有办法修改结构声明,所以调用3号会成功吗?

c++ pointers operator-overloading function-object

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

通过引用将二维数组传递给函数(C编程)

我正在学习指针,现在卡住了一个小时,用这段代码,

#include <stdio.h>

int determinant(int **mat)  /* int mat[3][3] works fine.. int *mat[3] doesn't.. neither does int *mat[] */
{
    int det;
    int a=*(*(mat+0)+0); // printf("\n%d",a);
    int b=*(*(mat+0)+1); // printf("\n%d",b);
    int c=*(*(mat+0)+2); // printf("\n%d",c);
    int d=*(*(mat+1)+0); // printf("\n%d",d);
    int e=*(*(mat+1)+1); // printf("\n%d",e);
    int f=*(*(mat+1)+2); // printf("\n%d",f);
    int g=*(*(mat+2)+0); // printf("\n%d",g);
    int h=*(*(mat+2)+1); // printf("\n%d",h);
    int i=*(*(mat+2)+2); // printf("\n%d",i);

    det = a*(e*i-h*f) - b*(d*i-g*f) + c*(d*h-e*g);
    return det;
}

int main()
{
    int mat[3][3];
    int i,j;
    printf("Enter the 3 X 3 …
Run Code Online (Sandbox Code Playgroud)

c arrays pointers function multidimensional-array

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

改变指针数组的内容

这似乎应该很容易,但我花了太多时间.希望有人可以提供帮助.

char *labels[] = { "apple", "orange" }; // each items inside label is string literal. We can't change them-
Run Code Online (Sandbox Code Playgroud)

看下面的内容

char a[]="hi";
char b[]="hello";
char *name[]={a,b};//each item inside name is not string literal rite??
*name="bye";
puts(a);
Run Code Online (Sandbox Code Playgroud)

我认为输出将是bye因为我改变了a[]使用的内容*name="bye"

但产量仍然是hi.为什么?

c arrays pointers

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

非const int指针在const int上的行为

#include<stdio.h>

int main()
{
       const int sum=100;
        int *p=(int *)&sum;

        *p=101; 


        printf("%d, %d",*p,sum);
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

/*

产量

101,101

*/

p指向一个常数整数变量,那么为什么/ p如何设法改变sum的值?

pointers

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