473,378 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

c++ pointers

hi i am providing my code to shift right and shift left as stated in the
comments. it does compile but it does not work for the pointers and does
not shift right. please help.

#include <iostream>
#include <cassert>
#include "ListA.h"
#include "ListA.cpp"

using std::cin;
using std::cout;
using std::endl;

void rotateRef(int &x, int &y, int &z, int direction, bool &success)
// If direction equals 0, the values of the variables are rotated to
// the right (i.e. y is set equal to x, z is set equal to y, and x is
// set equal to z). If direction equals 1, the values of the
// variables are rotated to the left. Success is set to true if a
// correct direction is entered, and to false otherwise.

{

int temp;

if (direction == 0)
{

temp=y;
y=x;
x=z;
z=temp;

}
else if (direction == 1)
{
temp=x;
x=y;
y=z;
z=temp;
}
}

void testRotateRef()
// Function to test rotateRef. Reads three values and a direction from
// the keyboard, passes them to rotateRef, and then prints the result.
// Prints a message if an error is reported by rotateRef.
{

int direction;
int x;
int y;
int z;
bool success;

cout<< "Enter three values followed by one space:"<< endl;
cin>>x;
cin>>y;
cin>>z;
cout<< "Enter 1 to totate right or 0 to rotate left"<<endl;
cin>>direction;

if (direction = 0)
{
rotateRef(x,y,z,direction,success);

cout <<x<<y<<z<<".\n";
}

else if (direction = 1)
{
rotateRef(x,y,z,direction,success);

cout <<x<<y<<z<<".\n";
}
}

void rotatePtr(int *xPtr, int *yPtr, int *zPtr, int direction, bool
&success)
// If direction equals 0, the values of the variables are rotated to the
// right. If direction equals 1, the values of the variables are
// rotated to the left. Success is set to true if a correct direction is
// entered, and to false otherwise.

{
int temp;

if (direction == 0)
{
success = true;

temp=*yPtr;
*yPtr=*xPtr;
*xPtr=*zPtr;
*zPtr=temp;

}
else if (direction == 1)
{
success = true;

temp=*xPtr;
*xPtr=*yPtr;
*yPtr=*zPtr;
*zPtr=temp;
}
else
{
(success = false);

}

}
void testRotatePtr()
// Function to test rotatePtr. Reads three values and a direction from
// the keyboard, passes them to rotateRef, and then prints the result.
// Prints a message if an error is reported by rotatePtr.

{

int direction;
int *xPtr;
int *yPtr;
int *zPtr;
bool success;


cout<< "Enter three values followed by one space:"<< endl;
cin>>*xPtr;
cin>>*yPtr;
cin>>*zPtr;

cout<< "Enter 1 to totate right or 0 to rotate left"<<endl;
cin>>direction;

if (direction = 0)
{
rotatePtr(xPtr,yPtr,zPtr,direction,success);
cout <<xPtr<<yPtr<<zPtr<<".\n";
}
else if (direction = 1)
{
rotatePtr(xPtr,yPtr,zPtr,direction,success);
cout <<xPtr<<yPtr<<zPtr<<".\n";
}
else
{
cout<<"Error"<<endl;
}
}
Jul 23 '05 #1
4 1592
chirag wrote:
hi i am providing my code to shift right and shift left as stated in the
comments. it does compile but it does not work for the pointers and does
not shift right. please help.
[...]
void testRotatePtr()
// Function to test rotatePtr. Reads three values and a direction from
// the keyboard, passes them to rotateRef, and then prints the result.
// Prints a message if an error is reported by rotatePtr.

{

int direction;
int *xPtr;
int *yPtr;
int *zPtr;
You declared three pointers. What do they point _to_? Nothing.
They are *uninitialised*.
bool success;


cout<< "Enter three values followed by one space:"<< endl;
cin>>*xPtr;
First thing you do is dereference an *uninitialised* pointer. What
does that do? Undefined. Nasal demons. A nasty letter to your mom.
Formatting of your hard drive. Make your pick. Anything after that
does not matter.

Don't declare/define pointers here. Declare/define your regular values
just like you do in testRotateRef. Just pass your ints by taking their
address into the 'rotatePtr':

rotatePtr(&x, &y, &z, ...
[...]


V
Jul 23 '05 #2
"chirag" <ch*******@yahoo.com> wrote in
news:d7******************************@localhost.ta lkaboutprogramming.com:
hi i am providing my code to shift right and shift left as stated in the
comments. it does compile but it does not work for the pointers and does
not shift right. please help.


[large snip]

Take a look at the prototypes for your two rotate functions. Your int
version takes reference-to-int as parameters. Your pointer version only
takes the pointers by value. You want to pass the pointers by reference as
well...
Jul 23 '05 #3
On Thu, 03 Feb 2005 18:02:37 -0500, chirag wrote:
hi i am providing my code to shift right and shift left as stated in the
comments. it does compile but it does not work for the pointers and does
not shift right. please help.
if (direction = 0)
if (direction == 0)
{
rotateRef(x,y,z,direction,success);

cout <<x<<y<<z<<".\n";
}

else if (direction = 1)
else if (direction == 1)
{
rotateRef(x,y,z,direction,success);

cout <<x<<y<<z<<".\n";
}
}

<snip>

if (direction = 0)
if (direction == 0)
{
rotatePtr(xPtr,yPtr,zPtr,direction,success);
cout <<xPtr<<yPtr<<zPtr<<".\n";
}
else if (direction = 1)
else if (direction == 1)
{
rotatePtr(xPtr,yPtr,zPtr,direction,success);
cout <<xPtr<<yPtr<<zPtr<<".\n";
}
else
{
cout<<"Error"<<endl;
}
}


HTH

- Jay

Jul 23 '05 #4
chirag wrote:

Just a few additional comments.
void rotateRef(int &x, int &y, int &z, int direction, bool &success)
// If direction equals 0, the values of the variables are rotated to
// the right (i.e. y is set equal to x, z is set equal to y, and x is
// set equal to z). If direction equals 1, the values of the
// variables are rotated to the left. Success is set to true if a
// correct direction is entered, and to false otherwise.
First, if a function changes a value, pass a pointer, not a reference
(see Stroustroup). This is a clear indication to the caller that values
can be modified. Second, since your function can fail, you should
return the status as a return value, and the type should be 'int', not
'bool'. You should return 0 on success, and a non-zero value otherwise.
The actual non-zero values are not important to clients of 'rotateRef',
but when you *test* the function, you can verify numerous error legs. A
status of 'false' does not indicate *why* the function failed, and
therefore makes testing different error legs a guessing game. Thirdly,
the specification of 'direction' is a little to strict. What might be
better is

Rotate values to the left by one place if the specified
'direction' is less than zero, or to the right if the
specified 'direction' is greater than zero. If
0 = 'direction' the specified arguments 'x', 'y', and 'z'
are not affected.

Then, since (if) the function has no notion of failure, you can
dispense with the return type completely. (Typically it goes without
saying that invalid (or null) pointers cause undefined behavior.)
void testRotateRef()
// Function to test rotateRef. Reads three values and a direction from // the keyboard, passes them to rotateRef, and then prints the result. // Prints a message if an error is reported by rotateRef.
Interactive tests for these kinds of (low-level) functions are tedious
if not useless. You want to structure your test driver so that 1) it is
automated, 2) it can verify the results, and 3) it can be used as a
regression test to ensure that changes to the implementation meet the
contract. You want something like this:

// some helpful macros
#define LOOP2_ASSERT(I,J,C) // print I and J, non-fatal assert C
#define L_ __LINE__

// and then
const struct {
int d_line; // source line number
int d_x; // initial x value
int d_y; // initial y value
int d_z; // initial z value
int d_direction; // direction of rotation
int d_expX; // expected x value
int d_expY; // expected y value
int d_expZ; // expected z value
int d_result; // expected result
} DATA[] = {
//Line X Y Z Dir Exp X Exp Y Exp Z Res
//---- --- --- --- --- ----- ----- ----- ---
{ L_, 1, 2, 3, 0, 3, 1, 2, 0, },
{ L_, 4, 5, 6, 1, 5, 6, 4, 0, },
// etc
};
enum { DATA_SIZE = sizeof DATA / sizeof *DATA };

for (int i = 0; i < DATA_SIZE; ++i) {
const int LINE = DATA[i].d_line;
const int DIRECTION = DATA[i].d_direction;
const int EXP_X = DATA[i].d_expX;
const int EXP_Y = DATA[i].d_expY;
const int EXP_Z = DATA[i].d_expZ;
const int RESULT = DATA[i].d_result;

int x = DATA[i].d_x;
int y = DATA[i].d_y;
int z = DATA[i].d_z;

int result = rotateRef(x, y, z, direction);
LOOP2_ASSERT(i, LINE, EXP_X == x);
LOOP2_ASSERT(i, LINE, EXP_Y == y);
LOOP2_ASSERT(i, LINE, EXP_Z == z);
LOOP2_ASSERT(i, LINE, RESULT == result);
}
void rotatePtr(int *xPtr, int *yPtr, int *zPtr, int direction, bool
&success)


At this point, you probably want to consider a template function:

template <typename T>
int rotateArgs(T *a, T *b, T *c, int direction);

/david

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.