需要帮助解决我面临的ptr-ptr问题 [英] Need help resolving this ptr-ptr problem I am facing

查看:88
本文介绍了需要帮助解决我面临的ptr-ptr问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

typedef struct 
{
    char name[10];
    int age;
}detailsStruct;

static int fnProcess(detailsStruct** detailsPtrPtr)
{
    detailsStruct detailsStr;

    strcpy_s(detailsStr.name,"Arjun");
    detailsStr.age = 25;

    *detailsPtrPtr = &detailsStr; 


    return 0;
}

int main()
{
    int rv = 0;
    detailsStruct* detailsPtr;

    rv= fnProcess(&detailsPtr);

    printf("\n Name %s", detailsPtr->name);
    printf("\n Age %d", detailsPtr->age);
    _getch();
    return rv;
}


这是我得到的输出:


Here is the output I am getting:

Name Arju£²▲
Age 221459990

推荐答案

您似乎有两个问题:
1. strcpy_s()函数具有三个参数,请查阅文档.
2. fnProcess()函数中的detailsStr是局部变量,因此返回时超出范围.至少在返回后使用其地址可能会产生不正确的结果,并可能导致程序崩溃.
You appear to have two problems:
1. The strcpy_s() function takes three parameters, check the documentation.
2. detailsStr in the fnProcess() function is a local variable and so goes out of scope when you return. Using its address after the return is likely, at the least, to produce incorrect results, and possibly crash your program.


这篇关于需要帮助解决我面临的ptr-ptr问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆