am confused about the output.

Mar 26, 2016 at 10:27am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
typedef char TEXT[80];
#include<iostream>
#include<string.h>
using namespace std;
void JumbleUp(TEXT T)
{
   int L=strlen(T);
   for(int C=0;C<L-1;C+=2)
   {
     char CT=T[C];
     T[C]=T[C+1];
     T[C+1]=CT;
   }
  for(int C=1;C<L;C+=2)
   if(T[C]>='M' && T[C]<='U')
      T[C]='@';
}
int main()
{
   TEXT Str="HARMONIOUS";
   JumbleUp(Str);
   cout<<Str<<endl;
   return 0;
}




ISN'T THIS CALL BY VALUE......if it is,then i should get output as HARMONIOUS becoz any changes made on the formal parameters does not get reflected in the actual parameters in CALL BY VALUE.
but am getting sumthing else . why ?
Last edited on Mar 26, 2016 at 10:28am
Mar 26, 2016 at 10:42am
You are passing a pointer, by value. Yes, I know, it looks like it should be some kind of char array being passed by value. It isn't.

So the function JumbleUp receives a copy of a pointer, pointing at the memory location containing "HARMONIOUS". So it works on that memory space.

There is no copy of "HARMONIOUS" made; only a copy of a pointer to it, and it doesn't matter that the pointer is a copy - it still points to the exact same memory.

If you were passing a proper C++ string, then that string would be copied. As an aside, why are you coding so heavily in C? Why not C++?
Last edited on Mar 26, 2016 at 10:48am
Mar 26, 2016 at 11:10am
Thanks for explaining but am still unclear. how do i know that a pointer is being passed ? and why is the char array not being passed ? am a beginner , so plz explain elaborately.

and this is c++ program.
Mar 26, 2016 at 11:58am
You say it's C++, but there's almost no C++ in it. It uses C style strings and an ugly typedef; you're clearly thinking in C.

Anyway, this is how the C and C++ language works. When you pass an array, what actually gets passed is a pointer to the start of the array.

how do i know that a pointer is being passed ?


You know this because when you were learning about C and C++, you read it somewhere or maybe someone told you.

I don't know how to say it more elaborately that this. It's how C and C++ work. When you pass an array, what actually gets passed is a a pointer to the start of the array.
Last edited on Mar 26, 2016 at 12:00pm
Topic archived. No new replies allowed.