Solved

Need help with pointer output using array and pointer notation

Posted on 2007-11-14
8
327 Views
Last Modified: 2013-12-14
Ok this is a continuation question.. I am using pointer notation with the output being in a function and the input being in the main body. Problem is it compiles works except the output is not the numbers reversed it is a weird output.

Here is my code:

#include <iostream>

using namespace std;

void arrayRev();

const int SIZE = 5;

int main()
{
      int numbers[SIZE];
      int* p_numbers;
      int counter;
      p_numbers = numbers;

      cout << "Enter five integers: ";

      for (counter = 0; counter < SIZE; counter++)
      {
            cin >> numbers[counter];
            cout << "Enter five integers: ";
      }
     
      cout << endl;

      arrayRev();

      return 0;
}
void arrayRev()
{
      int numbers[SIZE];
      int* p_numbers;
      int counter;
      p_numbers = numbers;

      cout << "The numbers in reverse are: ";

      for (counter = 4; counter >= 0; counter--)
            cout << *(p_numbers+counter) << " ";
      cout << endl;
}
0
Comment
Question by:jschmuff
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 8

Accepted Solution

by:
Anthony2000 earned 250 total points
ID: 20286301
The main problems that I see is that there are two separate arrays with the same name: numbers - you need to have only one, you could make it global by declaring it after the statement: const int SIZE = 5;
And remove the two declarations (one in main and the one in arrayRev
 
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 20286307
Do you understand why? This is important to know for future code writing.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20286314
Indeed you are not passing the list to the output function, so basically you are printing garbage, it should be something like:

int main()
{
      int numbers[SIZE];
      int* p_numbers;
      int counter;
      p_numbers = numbers;

      cout << "Enter five integers: ";

      for (counter = 0; counter < SIZE; counter++)
      {
            cin >> numbers[counter];
            cout << "Enter five integers: ";
      }
     
      cout << endl;

      arrayRev(numbers);

      return 0;
}
void arrayRev(int *p_numbers)
{
      int counter;

      cout << "The numbers in reverse are: ";

      for (counter = 4; counter >= 0; counter--)
            cout << *(p_numbers+counter) << " ";
      cout << endl;
}
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:jschmuff
ID: 20286326
That version of the code to try produced an error:

.\main.cpp(26) : error C2660: 'arrayRev' : function does not take 1 arguments
0
 

Author Comment

by:jschmuff
ID: 20286351
ok so I declared int numbers[SIZE]; //before main function

removed it from both arrayRev function and the main.

seems to work properly now. Thanks for the help and I do understand I knew once you said to declare it globally I was beginning to see the real problem with the code. Now that I got that working just curious would there be a way to turn this into a class dont worry this is my assignment I just wanna know if it would be possible just in case your not sure of my homework this is my assignment:

Write a C++ function ReverseArray using "pointer notation" that will write out the elements of an array of int in reverse order.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20286353
yeah, you need to change the function prototype too, to match the real function:

#include <iostream>
using namespace std;

void arrayRev(int *);  // <------------this must change too

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20286358
if you declare the array as global, then you don't need to pass as an argument, but this version is more reusable.
about the class, you can make a "StringReverser" class and put the function just made as a method.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20287533
>> ok so I declared int numbers[SIZE]; //before main function

There's no need for that. Passing it as a parameter is a lot better (see also your other thread where this has also been suggested a few times).

If you have trouble with function parameters, then I suggest to check this tutorial :

        http://www.cplusplus.com/doc/tutorial/functions.html
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
debug as  junit test 4 80
thread-safe code in c++ 2 98
How to convert MFC APP to Win32 APP. 19 79
Error creating a new C++ project in ,net 20 33
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now