2 Begginer questions...

This is a discussion on 2 Begginer questions... within the C++ Programming forums, part of the General Programming Boards category; Hey, this is my first post and im learning how to program in c++ As i advance in the begginer ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    Question 2 Begginer questions...

    Hey, this is my first post and im learning how to program in c++

    As i advance in the begginer lessons, 2 questions have been wondering inside my head:

    1. How do you use pointers to change, from function A, the value of a variable from another function B? (Or, how do you do it at all :P)

    2. How can u return structures from functions? IE:
    Code:
    database fn();
    Being database a predefined struct?
    What are the practical uses for this?

    Thanx man

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    1. You make the parameter of the function B a pointer, then pass the address of the variable to the function. In C++, it is usually easier to use references instead. In that case, you would make the parameter of B a reference, and you pass the variable from A normally. Any changes to the parameter in B will affect the variable in A.

    2. Your code is correct. There are too many uses for this to list them all or to even start. In C++, a very common design goal is to make structs and classes (which are virtually the same thing) act like built-in types. That means that you pass them to functions or return them from functions just as you would a type like int or double.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Thanx

    Man i get trouble when returning structures from functions...
    I get the following error message:

    Code:
    .\Starter.cpp(22) : error C2556: 'main::data fn(void)' : overloaded function differs only by return type from 'char fn(void)'
            .\Starter.cpp(6) : see declaration of 'fn'
    Last edited by SoulBlade; 01-18-2006 at 12:34 PM.

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    post some code
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Basically I define the struct, then create a function with return type of the struct type, but i dont know what to set in the return value of that function...

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    The error is because you have two functions both named fn, one that returns char and one that returns the struct.

    The code should be the same for a struct or an int. Create a variable of the type of your struct and return it. You really need to post the exact code if you have a specific problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21