I'm wanting to pass a local variable within a function, back through it's pointer parameter (not returned).
我想在函数中传递局部变量,通过它的指针参数(不返回)。
My assignment uses a stack data structure, and one criteria that must be used is the Pop() function must have a pointer parameter that is used to return the top-most item on the stack. I have used this before. My program became more complex with a data struct, I started getting either segmentation faults, or the data not being saved after the function's frame popped.
我的赋值使用堆栈数据结构,必须使用的一个条件是Pop()函数必须有一个指针参数,用于返回堆栈中最顶层的项目。我之前用过这个。我的程序使用数据结构变得更加复杂,我开始得到分段错误,或者在弹出函数框架后没有保存数据。
// Definitions
typedef char * string;
typedef enum { SUCCESS, FAIL } result;
typedef enum { INTEGER, DOUBLE, STRING } item_tag;
// Result Check
static result RESULT;
// Item_Tag
typedef struct {
item_tag tag;
union {
int i;
double d;
string s;
} value;
} item;
// Declarations
int STACK_SIZE = 0;
const int MAX_STACK_SIZE = 1024; // Maximum stack size
item stack[1024];
// Pop
result Pop(item *ip){
item poppedItem;
item * pointerReturn = malloc(sizeof(item));
// Check stack size is not 0
if(STACK_SIZE == 0){
return FAIL;
}
// If stack size is only 1, creates a blank stack
else if(STACK_SIZE == 1){
item emptyItem;
// Initialize
emptyItem.tag = INTEGER;
emptyItem.value.i = 0;
// Check top item's tag
poppedItem = stack[0];
// Store top item data based on tag
switch(stack[0].tag){
case STRING:
poppedItem.value.s = stack[0].value.s;
case DOUBLE:
poppedItem.value.d = stack[0].value.d;
default:
poppedItem.value.i = stack[0].value.i;
}
poppedItem.tag = stack[0].tag;
// Allocate memory for parameter, and have it point to poppedItem
ip = malloc(sizeof(poppedItem));
*ip = poppedItem;
// Store empty stack to top of stack
stack[0] = emptyItem;
// Decrease stack size
STACK_SIZE--;
}
// Grab top Item from stack
else{
// Check top item's tag
poppedItem = stack[0];
// Store top item data based on tag
switch(stack[0].tag){
case STRING:
poppedItem.value.s = stack[0].value.s;
case DOUBLE:
poppedItem.value.d = stack[0].value.d;
default:
poppedItem.value.i = stack[0].value.i;
}
poppedItem.tag = stack[0].tag;
// Allocate memory for parameter, and have it point to poppedItem
ip = malloc(sizeof(poppedItem));
*ip = poppedItem;
// Reshuffle Items in Stack
for(int idx = 0; idx < STACK_SIZE; idx++){
stack[idx] = stack[idx + 1];
}
STACK_SIZE--;
}
return SUCCESS;
}
My knowledge with pointers is alright, and memory location/management. But I can't claim to be an expert by any means. I don't exactly know what happens in the background when you're using the function's own pointer parameter as a means of passing data back.
我的指针知识很好,内存位置/管理。但我不能声称自己是专家。当你使用函数自己的指针参数作为传递数据的方法时,我并不完全知道后台会发生什么。
- What is the correct syntax to solve this problem?
- How can a parameter pass something back?
解决此问题的正确语法是什么?
参数怎么能传回来?
Thanks in advance!
提前致谢!
EDIT* Since many people are confused. I'll post some snippets. This is an assignment, so I cannot simply post all of it online as that'd be inappropriate. But I think it's okay to post the function itself and have people analyze it. I'm aware it's a bit messy atm since I've edited it several dozen times to try and figure out the solution. Sorry for the confusion. Keep in mind that not all the code is there. just the function in question, and some of the structure.
编辑*因为很多人都很困惑。我会发一些片段。这是一项任务,因此我不能简单地在线发布所有内容,因为这是不合适的。但我认为发布功能本身并让人们分析它是可以的。我知道它有点乱,因为我已经编辑了几十次以试图找出解决方案。对困惑感到抱歉。请记住,并非所有代码都存在。只是有问题的功能,以及一些结构。
2 个解决方案
#1
2
The function should receive a pointer to a valid object:
该函数应该接收一个指向有效对象的指针:
item catcher;
myFunc(&catcher); // Pass a pointer to catcher
and the function should modify the object it received a pointer to:
并且该函数应该修改它收到指针的对象:
void myFunc(item *itemPointer)
{
itemPointer->variable = stuff;
// or
*itemPointer = someItem;
}
Update:
You're overcomplicating things immensely – there should be no malloc
s when popping, and you're leaking memory all over the place.
(Your knowledge of pointers and memory management is far from "alright". It looks more like a novice's guesswork than knowledge.)
你的东西太过复杂了 - 弹出时应该没有mallocs,而且你在整个地方都在泄漏内存。 (你对指针和内存管理的知识远非“好”。它看起来更像是新手的猜测而不是知识。)
It should be something more like this:
它应该是更像这样的东西:
result Pop(item *ip){
if (STACK_SIZE == 0){
return FAIL;
}
else {
*ip = stack[0];
for(int idx = 0; idx < STACK_SIZE; idx++){
stack[idx] = stack[idx + 1];
}
STACK_SIZE--;
}
return SUCCESS;
}
but it's better to push/pop at the far end of the array:
但最好在阵列的远端推/弹:
result Pop(item *ip){
if (STACK_SIZE == 0){
return FAIL;
}
else {
*ip = stack[STACK_SIZE-1];
STACK_SIZE--;
}
return SUCCESS;
}
#2
0
Response to the originally posted code:
对最初发布的代码的回复:
typedef struct{
variables
}item;
void myFunc(item *itemPointer){
item newItem;
newItem.variable = stuff;
}
int main(){
item * catcher;
myFunc(catcher);
printf("%s\n", catcher.variable);
}
A few issues.
一些问题。
Your program will not compile. variable
has to have a type.
你的程序将无法编译。变量必须有一个类型。
void myFunc(item *itemPointer){
item newItem;
newItem.variable = stuff;
}
stuff
is not defined; item *itemPointer
is not used.
东西没有定义; item * itemPointer未使用。
item * catcher
pointer has to point to allocated memory. It is not initialized.
item * catcher指针必须指向已分配的内存。它没有初始化。
Pass arguments via pointers and modify member of the structure like this:
通过指针传递参数并修改结构的成员,如下所示:
void myFunc(item *itemPointer, const char *string){
itemPointer->variable = string ;
}
Solution like:
void myFunc(item *itemPointer)
{
itemPointer->variable = stuff;
// or
*itemPointer = someItem;
}
is possible, but it assumes that stuff
or someItem
is a global variable
which is not the best programming practice IMO.
是可能的,但它假设stuff或someItem是一个全局变量,它不是最好的编程实践IMO。
Retrieve value from pointer via ->
not .
operator.
通过 - > not从指针检索值。运营商。
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char * variable;
}item;
void myFunc(item *itemPointer, const char *string){
itemPointer->variable = string ;
}
int main(){
item * catcher;
char *new_string = "new string";
catcher = malloc(sizeof(item));
myFunc(catcher, new_string);
printf("%s\n", catcher->variable);
free(catcher);
return 0;
}
OUTPUT:
new string
#1
2
The function should receive a pointer to a valid object:
该函数应该接收一个指向有效对象的指针:
item catcher;
myFunc(&catcher); // Pass a pointer to catcher
and the function should modify the object it received a pointer to:
并且该函数应该修改它收到指针的对象:
void myFunc(item *itemPointer)
{
itemPointer->variable = stuff;
// or
*itemPointer = someItem;
}
Update:
You're overcomplicating things immensely – there should be no malloc
s when popping, and you're leaking memory all over the place.
(Your knowledge of pointers and memory management is far from "alright". It looks more like a novice's guesswork than knowledge.)
你的东西太过复杂了 - 弹出时应该没有mallocs,而且你在整个地方都在泄漏内存。 (你对指针和内存管理的知识远非“好”。它看起来更像是新手的猜测而不是知识。)
It should be something more like this:
它应该是更像这样的东西:
result Pop(item *ip){
if (STACK_SIZE == 0){
return FAIL;
}
else {
*ip = stack[0];
for(int idx = 0; idx < STACK_SIZE; idx++){
stack[idx] = stack[idx + 1];
}
STACK_SIZE--;
}
return SUCCESS;
}
but it's better to push/pop at the far end of the array:
但最好在阵列的远端推/弹:
result Pop(item *ip){
if (STACK_SIZE == 0){
return FAIL;
}
else {
*ip = stack[STACK_SIZE-1];
STACK_SIZE--;
}
return SUCCESS;
}
#2
0
Response to the originally posted code:
对最初发布的代码的回复:
typedef struct{
variables
}item;
void myFunc(item *itemPointer){
item newItem;
newItem.variable = stuff;
}
int main(){
item * catcher;
myFunc(catcher);
printf("%s\n", catcher.variable);
}
A few issues.
一些问题。
Your program will not compile. variable
has to have a type.
你的程序将无法编译。变量必须有一个类型。
void myFunc(item *itemPointer){
item newItem;
newItem.variable = stuff;
}
stuff
is not defined; item *itemPointer
is not used.
东西没有定义; item * itemPointer未使用。
item * catcher
pointer has to point to allocated memory. It is not initialized.
item * catcher指针必须指向已分配的内存。它没有初始化。
Pass arguments via pointers and modify member of the structure like this:
通过指针传递参数并修改结构的成员,如下所示:
void myFunc(item *itemPointer, const char *string){
itemPointer->variable = string ;
}
Solution like:
void myFunc(item *itemPointer)
{
itemPointer->variable = stuff;
// or
*itemPointer = someItem;
}
is possible, but it assumes that stuff
or someItem
is a global variable
which is not the best programming practice IMO.
是可能的,但它假设stuff或someItem是一个全局变量,它不是最好的编程实践IMO。
Retrieve value from pointer via ->
not .
operator.
通过 - > not从指针检索值。运营商。
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char * variable;
}item;
void myFunc(item *itemPointer, const char *string){
itemPointer->variable = string ;
}
int main(){
item * catcher;
char *new_string = "new string";
catcher = malloc(sizeof(item));
myFunc(catcher, new_string);
printf("%s\n", catcher->variable);
free(catcher);
return 0;
}
OUTPUT:
new string