Hi, I am unable to understand why is the output for this shows me the address of the stored array.
int main()
{
size_t k= 0 ;
char t[3][100] = { "hi", "di", "fi" } ;
char s[3][100] ;
for (int r = 0 ; r < 3 ; r++)
{
size_t k = strlen (t[r]);
for ( int c = 0 ; c < k ; c++)
{
s[r][c] = t[r][c] ;
// cout << s[r][c] ; // this works perfect to show me the output
}
}
How can you get output from a program that cannot be compiled successfully? What compiler are you using (version and number)?
First, even if you add the missing headers, the code should not have compiled:
Code:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
size_t k= 0 ;
char t[3][100] = { "hi", "di", "fi" } ;
char s[3][100] ;
for (int r = 0 ; r < 3 ; r++)
{
size_t k = strlen (t[r]);
for ( int c = 0 ; c < k ; c++)
{
s[r][c] = t[r][c] ;
}
}
cout << s[r][c] ; // <--- How did this line compile??
}
This is not valid ANSI C++ code. The variables "r" and "c" are not available outside of where they were declared as loop counters. So how could you have created a program with incorrect C++ syntax?
for this shows me the address of the stored array.
For me, it shows nothing since no program was even created due to syntax errors.
Now, if you define 'r' and 'c' outside the first for loop, at this point it shouldn't show any specific values -
cout << s[r][c] ;
coz, at this point it will be out of the range of the array s - here the line will get resolved to -
cout << s[3][2] ; <== whcih yet not get assigned
Hope, my points are clear here...
Raju2001006
Please put [CODE][/CODE] tags around your code to preserve indentation and make it more readable...
... here the line will get resolved to -
cout << s[3][2] ; <== whcih yet not get assigned
Hope, my points are clear here...
I have no idea why you need to output the value of some array element outside the loop, however, using 3 as the first index of array s is illegal: only indexes 0 to 2 are valid since this array has only 3 rows.
Bookmarks