-
display variables
i got a problem in displaying variables.
i wrote this code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int main()
{
int x;
int *pointer;
pointer=&x;
scanf("%ld", x);
printf ("*pointer");
return 0;
}
but the command printf isnt displaying the variable.
maybe this c++ code helps:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int main()
{
int x;
int *pointer;
pointer=&x;
cin>>x;
cout<<*pointer;
return 0;
}
but i need this c++ code in ordinary C. whats the translation for "cout << *pointer"?
-
Why do you include so many header files without using them? Anyway, your problem is that you were using both scanf and printf wrong:
Code:
#include <stdio.h>
int main()
{
int x;
int *pointer;
pointer = &x;
scanf ("%d", &x);
printf ("%d\n", *pointer);
return 0;
}
scanf takes the address of non-array types. If you forget this then the program will seg fault. Also, %ld designates a long integer, not an integer, with scanf this distinction is very important so you should get into the habit of using the correct flag. printf takes format flags as well, the way you had it the only thing printed to the screen would be "*pointer".
-Prelude
-
I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you ;)
For example:
Without code tags:
for(int i=0;i<5;i++)
{
cout << "No code tags are bad";
}
With Code Tags:
Code:
for(int i=0;i<5;i++)
{
cout << "This code is easy to read";
}
This is of course a basic example...more complicated code is even easier to read with code tags than without.
I've added code tags for you this time. They can be added by putting [ code ] at the beginning of your code and [ /code ] at the end, only without the spaces. More information on code tags may be found at the link in my signature. Any further questions or ways I can help please feel free to PM me.