Jump to content
  • Advertisement
Sign in to follow this  
MadScientist

C++ Passing a struct to a function

This topic is 2760 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know how to pass a variable to a function through a pointer parameter to change its value. See code:
void Inc(int *var)
{
*var=*var+1;
}
I need to be able to pass struct to a function in a similar way so that its contents can be changed. Would it be something like this?
(I know it doesn't work, just shows the concept.)
#include "stdafx.h"
#include <iostream>
#include "Stdio.h"

struct test {
int num;
};


void Inc(test *var);

int _tmain(int argc, _TCHAR* argv[])
{
test i;
i.num=5;

Inc(&i);

//Should print "6"
printf("%d\n",i);
system("pause");

return 0;
}

void Inc(test *var)
{
*var.num=*var.num+1;
}

By the way what are the tags to make code in a code box?

Share this post


Link to post
Share on other sites
Advertisement
Instead of using a pointer in the function parameters try a reference & like this

void Inc(Test& Var)
{
Var.num += 1;
}

should increment to 6 for you.

Share this post


Link to post
Share on other sites
The problem you're having is one of syntax. You need to dereference "var" before using the member access operator, like so:

void Inc(test *var)
{
(*var).num = (*var).num + 1;
}


Better, use the arrow operator, which is shorthand:

void Inc(test *var)
{
var->num = var->num + 1;
}


Full program example, fixing other errors:

#include <iostream>
#include <cstdio>

struct test
{
int num;
};

void Inc(test *var);

int main(int argc, const char* argv[])
{
test i;
i.num=5;

Inc(&i);

// Should print "6"
printf("%d\n",i.num);
}

void Inc(test *var)
{
var->num = var->num + 1;
}



Better still, use references as wicked357 suggested.

Share this post


Link to post
Share on other sites
Use references were ever possible, it allows the compiler to do its job better of optimizing. The reason is that a reference cannot change, but a pointer can change. For example

void func1(int& k){
k+=1;// easy enough, add 1 onto k
}
void func2(int* k){
k+=1;// increment the address of k to point 1 byte after where it was previously pointing ..
*k+=1;// now, what ever k is pointing to is changed....whats there? I dont know, maybe the compiler doesn't either
}

now, in func1, k is actually the int passed to the function, and in func2, i changed the address of what k was pointing to and the compiler may not know what is at that address, so it will leave this until run-time. These are extremely simple examples that will show you how if something is known at compile time, the compiler should do its job and figure it out, but if it is not known until run-time, then the compiler cannot do its job.


So, use references were ever possible..... points only where its absolutely needed.

Share this post


Link to post
Share on other sites
Quote:
Original post by smasherprog
Use references were ever possible, it allows the compiler to do its job better of optimizing. The reason is that a reference cannot change, but a pointer can change.
Much more importantly, a reference can never be null, and a reference can never point to invalid memory*. When you use a pointer you need to account for the possibility that you are passed a null or invalid pointer, so references both reduce effort and increase the robustness of your code.

*This is not entirely true: it is possible to construct null/invalid references, but not without some very obvious intent. It isn't going to happen accidentally.

Share this post


Link to post
Share on other sites
I have a question, I am sorry if I should have made my own topic this one made me think of the question. What would happen if you were to do something along the lines of:

int inc(int& Val){
return (Val++);
}

int main(){
inc(7);
return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by 4th_dimention
I have a question, I am sorry if I should have made my own topic this one made me think of the question. What would happen if you were to do something along the lines of:

int inc(int& Val){
return (Val++);
}

int main(){
inc(7);
return 0;
}


Suggestion: just do it... Type it, compile it (if you can, this you cannot) and, after that, debug it step by step, using the watch to see all the values in question. You will learn some new things from it.

Be brave :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!