C ++函数返回指针到结构变得很奇怪 [英] C++ Function Return Pointer to Struct Goes Weird

查看:137
本文介绍了C ++函数返回指针到结构变得很奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个任务,创建一个程序,可以使用链表从文本文件中读取多项式。其中,当我试图从read_poly函数返回链表poly_pointer的开始指针很奇怪。





预期输出应为-12





但是我得到的是-10



如果我在read_poly中返回前添加一行代码



  cout< curr_ptr-> coef; 

输出将突然变为2-12
任何人都可以提供一些解释为什么和如何解决这个问题?



Polynomial.h

  #ifndef _POLYNOMIAL_H_ 
#define _POLYNOMIAL_H_

using namespace std;

typedef struct poly_node * poly_pointer;

typedef struct poly_node {
int coef;
int expon;
poly_pointer link;
};

poly_pointer addNode(int coef,int expon);

#endif

Polynomial.cpp

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

poly_pointer addNode(int coef,int expon)
{
poly_node a;
poly_pointer ptr =& a;
a.coef = coef;
a.expon = expon;
return ptr;
}

poly_pointer read_poly(const char * fileName)
{
poly_pointer start_ptr,curr_ptr;
start_ptr = curr_ptr = addNode(-1,6);
curr_ptr = curr_ptr-> link = addNode(2,3);
return start_ptr;
}

main.cpp

  #include< iostream> 
#includePolynomial.h
using namespace std;

int main(void)
{
poly_pointer a,b,d,e,f;
a = read_poly(input1.txt);
cout<< a-> coef;
cout<< a-> link-> coef;

cout<< \\\
-eop-;
cin.get();
return 0;
}


解决方案

addNode本地分配的poly_node。



以下是内存的稀疏分配,但会工作。

  poly_pointer a = new poly_pointer(); 
a-> coef = coef;
a-> expon = expon;
return a;


I am working on an assignment which to create a program that can read a polynomial from a text file using linked list. Which when i tried to return the starting pointer of the linked list "poly_pointer" from the read_poly function things being weird.

The expected output should be -12

But what i got is -10

And if i add one single line of code right before return in read_poly

cout << curr_ptr->coef;

the output would suddenly turns to 2-12 May anyone provide some explanation on why and how to fix this problem?

Polynomial.h

#ifndef _POLYNOMIAL_H_
#define _POLYNOMIAL_H_

using namespace std;

typedef struct poly_node *poly_pointer;

typedef struct poly_node {
  int coef;
  int expon;
  poly_pointer link;
};

poly_pointer addNode(int coef, int expon);

#endif

Polynomial.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "Polynomial.h"
using namespace std;

poly_pointer addNode(int coef, int expon)
{
    poly_node a;
    poly_pointer ptr = &a;
    a.coef = coef;
    a.expon = expon;
    return ptr;
}

poly_pointer read_poly(const char* fileName)
{
    poly_pointer start_ptr, curr_ptr;
    start_ptr = curr_ptr = addNode(-1, 6);
    curr_ptr = curr_ptr->link = addNode(2, 3);
    return start_ptr;
}

main.cpp

#include <iostream>
#include "Polynomial.h"
using namespace std;

int main(void)
{
    poly_pointer a, b, d, e, f;
    a = read_poly("input1.txt");
    cout << a->coef;
    cout << a->link->coef;

    cout << "\n-eop-";
    cin.get();
    return 0;
}

解决方案

addNode is returning a pointer to a locally allocated poly_node.

The following is sloppy allocation of memory, but will work.

  poly_pointer a = new poly_pointer();
  a->coef = coef;
  a->expon = expon;
  return a;

这篇关于C ++函数返回指针到结构变得很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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