Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,495 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,360 people online right now. Registration is fast and FREE... Join Now!




Pointers

 
Reply to this topicStart new topic

Pointers

krazykrisi
22 Mar, 2007 - 10:25 AM
Post #1

New D.I.C Head
*

Joined: 16 Jan, 2007
Posts: 8


My Contributions
I am making this program with the following criteria:

Create a C++ program that does the following:

• Create an array of 5 integers and initialize it.
• Create an external function that accepts a memory address that points to an integer element of the above array.
Add 5 to the passed value.
• Output the contents of the array.

I am getting errors and I have no idea why.

Errors:
CODE
error C3861: 'func': identifier not found
error C2365: 'func' : redefinition; previous definition was 'formerly unknown identifier'


CODE
#include <iostream>
using namespace std;

int main()
{
    int arr1[5] = { 23, 14, 26, 21, 19 };
    func(arr1);
    for(int j=0; j<5; j++)
        cout << arr1[j] << " ";
    cout << endl;
    int x;
    cin >> x;
    return 0;
}
//--------------------------------------------------------------------------------
void func(int* arry)
{
    arry += 5;
}

User is offlineProfile CardPM
+Quote Post

horace
RE: Pointers
22 Mar, 2007 - 11:21 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
C++ has to know about a function before you can call it, e.g. its name and type and number of parameters. You can either move the code of func() before main() or use a function prototype which would be
CODE

void func(int* arry);


User is offlineProfile CardPM
+Quote Post

ByteWyse
RE: Pointers
22 Mar, 2007 - 11:32 AM
Post #3

New D.I.C Head
*

Joined: 2 Jan, 2007
Posts: 41


My Contributions
At the point in your program at which your function func() is first referenced, it has not yet been defined. The simplest solution is to add a declaration of the function prior to its first reference.

CODE
#include <iostream>
using namespace std;

// Forward declaration of func() to satisfy the compiler.
void func(int* arry);

int main()
{
    int arr1[5] = { 23, 14, 26, 21, 19 };
    func(arr1);
    for(int j=0; j<5; j++)
        cout << arr1[j] << " ";
    cout << endl;
    int x;
    cin >> x;
    return 0;
}
//--------------------------------------------------------------------------------
void func(int* arry)
{
    arry += 5;
}




User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:00PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month