Output and explanation req...

rahulmaximus's Avatar, Join Date: Jun 2011
Go4Expert Member
The following 'C' code prints:
void f(char *x) {
x++;
*x = ‘a’;
}
int main() {
char *str = "hello";
f(str);
printf("%s", str);
}
a) hello
b) allo
c) hallo
d) empty string
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
None of the answers is correct because
Code:
char *str="hello";
defines the string "hello" in a part of memory that cannot be modified by the program. What the program SHOULD do is this:
Code:
char str[32];
strcpy(str,"Hello");
and then the output would be "Hallo".
rahulmaximus's Avatar, Join Date: Jun 2011
Go4Expert Member
thanks but i got what u meant....but could answer this one then....

inside the func when i call printf 2nd time i get a total vague address value which does nt follow the character pointer arithmetic...it has no similarity to the first printf address.....
Code:
#include<iostream>
#include<stdio.h>

using namespace std;

void func(char ** x)
{    cout<<"\nThis is inside the small function :)\n\n";
     printf("The value whic is stored in x initially:%u\n",*x);
     *x++;
     printf("The value whic is stored in x afeterwards:%u\n",*x);
     
     **x='e';
}

int main()
{
     char *a="Rahul";
     printf("The address stored in a is :%u\n",a);
     printf("\nThe address stored in the next element is :%u\n",a+1);
     func(&a);
     cout<<a;
     system("pause");
}

Last edited by shabbir; 14Aug2011 at 17:13.. Reason: Code blocks
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
What is the exact output that you get when you run this program?
rahulmaximus's Avatar, Join Date: Jun 2011
Go4Expert Member
The address stored in a is :4199312

The address stored in the next element is :4199313

This is inside the small function

The value whic is stored in x initially:4199312
The value whic is stored in x afeterwards:2293680



****the prob******
i was expecting the last output to be 4199313....as its a character pointer....so the address should have increased by +1
rahulmaximus's Avatar, Join Date: Jun 2011
Go4Expert Member
But as u can see i got 2293680...which i din understand at all

but whereas in the main function u get 4199313...

but why is there an address anomaly in the main function and inside the func function...though in both we point to the same 2nd element ...atleast i think so


And i checked u were right we could not alter the content of a constant string thanks for tht ...but it seems so vague
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
*x is of what type? And what exactly happens when you ++ one of those?

You're not doing exactly the same thing; a+1 is not the same as *x++. And that is the cause of the different behaviour.
rahulmaximus's Avatar, Join Date: Jun 2011
Go4Expert Member
well **x is a pointer to a pointer...

so *x should contain the address of the element....
and yah i tried with a++ also same results...

i tried the code in dev c++.................
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
No, if **x is a pointer to a pointer, then *x is not what the pointer to the pointer double-points at; that would be **x. *x points to the POINTER and is of type POINTER, which is 4 bytes on a 32-bit system. So *x++ increases x by 4, not by 1, and does not point to the R. That's why you get a "garbage" value.
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
No, hang on, I got that wrong. If x is of type char** then *x will be of type char*.

Running through the code in the debugger, replacing the code to avoid undefined behaviour as follows:
Code:
     // char *a="Rahul";
	 char str[32];
	 strcpy(str,"Rahul");
	 char *a=str;
Before *x++, x contains 5175140. After that statement it contains 5175132 (I'm using Visual Studio 2010 on 64-bit Windows). The output displayed is:
The value whic is stored in x initially:5175140
The value whic is stored in x afeterwards:3435973836

and 3435973836=0xcccccccc, which is the value the Visual Studio debugger loads into certain places to help you spot broken pointer manipulation. Note however that 0xcccccccc is not the value in x but the value pointed at by x.

Did you know that *x++ is equivalent to *(x++)? Replacing *x++ with *(x++) gets the same behaviour. However replacing it with (*x)++ gets what I suspect to be the behaviour you want:

Code:
void func(char ** x)
{    cout<<"\nThis is inside the small function :)\n\n";
     printf("The value whic is stored in x initially:%u\n",*x);
     (*x)++;
     printf("The value whic is stored in x afeterwards:%u\n",*x);
     
     **x='e';
}

int main()
{
     // char *a="Rahul";
	 char str[32];
	 strcpy(str,"Rahul");
	 char *a=str;
     printf("The address stored in a is :%u\n",a);
     printf("\nThe address stored in the next element is :%u\n",a+1);
     func(&a);
	 printf("str after func is '%s'\n",str);
     //cout<<a;
     //system("pause");
}
Output:
Code:
The address stored in a is :2948836

The address stored in the next element is :2948837

This is inside the small function :)

The value whic is stored in x initially:2948836
The value whic is stored in x afeterwards:2948837
str after func is 'Rehul'