标签: pointers

如何在c中从其指针设置结构成员

如果我使用其地址将结构类型传递给函数,如何在结构中设置成员?

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

typedef struct foo {int avalue} foo_t;

void setmember(foo_t *foo_item){
    foo_item.avalue = 42;
}

int main(void) {
    foo_t dafoo;
    setmember(&dafoo);
    printf("%d\n", dafoo.avalue);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我按原样尝试,我会收到以下错误.

错误:在非结构或联合的东西中请求成员'avalue'

我理解错误,我不知道修复.我试过去引用:

*foo_item.avalue = 42;
Run Code Online (Sandbox Code Playgroud)

c struct pointers

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

指向python中的指针

我是python的新手,我试图理解如何将Python与C/C++联系起来.考虑问题:检查给定的链接列表是否是回文.从这个来源,我发现了一个非常有效的解决方案:

// Initial parameters to this function are &head and head
bool isPalindromeUtil(struct node **left, struct  node *right)
{
   /* stop recursion when right becomes NULL */
   if (right == NULL)
      return true;

   /* If sub-list is not palindrome then no need to
       check for current left and right, return false */
   bool isp = isPalindromeUtil(left, right->next);
   if (isp == false)
      return false;

   /* Check values at current left and right */
   bool isp1 = (right->data == (*left)->data); …
Run Code Online (Sandbox Code Playgroud)

c python pointers python-3.x

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

C编译器省略代码,没有错误

我正在从"学习C艰难的方式"做指针练习,其中一个额外的功劳是以相反的顺序打印循环,无论如何,我正在努力让练习正确,我想出了这个代码:

#include <stdio.h>

int main(int argc, char *argv[])
{
    // create two arrays we care about
    int ages[] = {23, 43, 12, 89, 2};
    char *names[] = {
        "Alan", "Frank",
        "Mary", "John", "Lisa"
    };
    // safely get the size of ages
    int count = sizeof(ages) / sizeof(int);

    // set up the pointers to the start of the arrays
    int *cur_age = ages;
    char **cur_name = names;

    // fourth way with pointers in a stupid complex way
    for(cur_name = names, cur_age …
Run Code Online (Sandbox Code Playgroud)

c gcc pointers

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

指针声明传染媒介

看看我教授给我的一些代码,我不明白发生了什么.我是编程新手,完全迷失了.

vector <_Account*>*myvector = nullptr;
Run Code Online (Sandbox Code Playgroud)

所以我知道他创建了一个向量,我知道一个现有的类,Account所以这是一个vector指向Account对象的指针吗?我不知道第二个星号是做什么的?

c++ pointers vector nullptr

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

malloc返回内存的免费范围

给出以下行:

 int *digits = (int *) malloc(3 * sizeof(int));
Run Code Online (Sandbox Code Playgroud)

假设我们将值1,2和3存储在位置,数字[0],数字[1],数字[2]中.如下:

digits[0] = 1;
digits[1] = 2;
digits[2] = 3;
Run Code Online (Sandbox Code Playgroud)

如果调用以下行:

free(++digits);
Run Code Online (Sandbox Code Playgroud)

malloc返回的整个内存范围是释放的,还是只是数字当前指向的int大小的块- 那时,数字[1]?或者是通过迭代释放整个范围的正确方法,即:

for (i = 0; i < 3; i++)
{
    free(digits[i]);
}
Run Code Online (Sandbox Code Playgroud)

我想了解免费电话的范围.malloc返回的整个内存块是释放的,还是只是一个子部分,当前由指针数字引用被释放?

c malloc pointers memory-management

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

如何更新函数外的数组(指向指针)

我有一个函数,它作为输入,指向2D数组的指针和指向1D数组的指针.

int resize(double *x, double **y, int n){
Run Code Online (Sandbox Code Playgroud)

此函数的目的是将x和y的大小调整为其长度(n)的两倍.

我创建了两个新的阵列 - 长度增加了一倍

double **yTemp = NULL;
double *xTemp = NULL;
xTemp = new double[2*n + 1];
yTemp = new double*[2*n + 1];
Run Code Online (Sandbox Code Playgroud)

然后我遍历并用x和y替换xTemp和yTemp的值

然后将它们设置为等于另一个:

        y = yTemp;
        x = xTemp;
        return 2*n;
Run Code Online (Sandbox Code Playgroud)

并退出该函数,y和x似乎失去了额外的长度.

任何有关这方面的帮助都会很棒!

c++ pointers

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

在函数中创建struct对象并传递其指针

我试图将一个空的struct指针传入一个名为"initialize"的函数作为输出参数,然后在函数完成后我可以检索struct指针指向的对象.一些意想不到的行为发生,我不太明白.

以下是我的代码:

static void initialize(complex_struct*& infoPtr)
{
    complex_struct myInfo;
    infoPtr = &myInfo;
    // some other codes that modify the contents of myInfo
    infoPtr->input_components = 3;
}

static void myfunction()
{
    complex_struct* infoPtr = nullptr;
    initialize(infoPtr);
    std::cout << infoPtr->input_components << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这个输出是一个任意的大整数.但我期待打印整数3.

限制:我无法修改complex_struct; 它是一个需要使用的third_party结构.另外,我需要将initialize()作为一个单独的函数的原因是我正在对complex_struct的内存分配进行性能测量.因此,将initialize()中的第一行代码移动到myfunction()并不是一个理想的解决方案.

c++ struct pointers

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

SIGSEGV短C代码错误

所以,作为一个对C很新的人,我遇到了我的第一个SIGSEGV错误.它出现在一个简短的C程序中,意在是一个"猜数字"游戏.它由一个自定义函数组成,它将两个数字和一个do-while循环与一个输入进行比较.start和do-while循环:

#include<stdio.h>

int checkNum(int num1, int num2); //See below for explanation

int main(void) {
    int input=0, rand=3; //"Random" number has fixed value for testing

    do {
        printf("Enter number from 0-10: "); //There is not actual range yet
        scanf("%d",input); //Get input
    } while(checkNum(input, rand)); //Checks if difference != 0
}
Run Code Online (Sandbox Code Playgroud)

比较功能:

//Function for comparing input with "random" number
int checkNum(int num1,int num2) { //The two numbers that get compared; First one: input, Second one: "random" rumber
    if(num1==num2) {
        printf("Correct. The random …
Run Code Online (Sandbox Code Playgroud)

c pointers function segmentation-fault

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

在分配指针时,memcpy与仅分配之间有什么区别?

例如,我有以下指针:

char *ptr;
char *string;

string = malloc(sizeof(char) * 10);
//set string to some word
Run Code Online (Sandbox Code Playgroud)

以下几行有什么区别?

ptr = string;

vs.

memcpy(ptr, string, sizeof(string));
Run Code Online (Sandbox Code Playgroud)

此外,如果我尝试在这些任务中的任何一个之后释放"字符串",是否会出现问题?"ptr"仍会保留我指定给它的值吗?

c pointers variable-assignment

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

如何检测接口{}是否为指针?

我想知道你怎么知道接口是否是类型指针.

package main

import "fmt"
import "reflect"

type str struct {
    a, b string
}

func main() {
    var s str
    x := &s
    t := reflect.TypeOf(interface{}(x))
    fmt.Printf("%v", t.Size())
}
Run Code Online (Sandbox Code Playgroud)

pointers go

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