who can find the Errors in this statments ;
1-

Assume that: char str[ 5 ];

cin >> str; // user types "hello"

2-

int *zPtr;        // zPtr will reference array z
int *aPtr = 0;
void *sPtr = 0;
int number;
int z[ 5 ] = { 1, 2, 3, 4, 5 };



++zPtr;


// use pointer to get first value of array
number = zPtr;



// assign array element 2 (the value 3) to number
number = *zPtr[ 2 ];



// print entire array z
for ( int i = 0; i <= 5; i++ )
   cout << zPtr[ i ] << endl;



// assign the value pointed to by sPtr to number
number = *sPtr;


++z;


char s[ 10 ];
cout << strncpy( s, "hello", 5 ) << endl;



char s[ 12 ];
strcpy( s, "Welcome Home");



if ( strcmp( string1, string2 ) )
   cout << "The strings are equal" << endl;

line 9: zPtr just contains some random address value, so incrementing on line 9 is meaningless.

line 18: thats just wrong -- use either *zPtr or zPtr, but not both at the same time.

7.14 Find the error(s) in each of the following statements:

Assume that: char str[ 5 ];

cin >> str; // user types "hello"

works great for me using vc++ 2008 express

#include <iostream>
using namespace std;

int main()
{
char str[25];
cin >> str; // user types "hello"Assume that: char str[ 5 ];

cout << str << "\n";
}

The only thing I can think of why your book thinks there is an error is that it should be std::cin >> str; The program would need the std:: part if you didn't state using namespace std as I did at the top of the program.

Oh, now I see what's wrong with it. str[5] doesn't have enough room to hold "Hello", which is 5 characters plus 1 for the string's null terminator. str[6] is the minimum for "Hello".

I thought that ,, but [5] means from 0-5 which equal 6 .. and "Hello" needs 5 +1 for null .so it is 6 same [5]; ? ! ?

correct me please ...

This article has been dead for over six months. Start a new discussion instead.