Why will this code not work?
#include <stdio.h>
#include <stdlib.h>
int newvalue(t);
int main(int argc, char *argv[])
{
int x = 3;
newvalue(&x);
printf("%i\n",x);
system("pause");
}
int newvalue(t)
{
int *t;
*t = 5;
return 0;
}
Why will this code not work?
#include <stdio.h>
#include <stdlib.h>
int newvalue(t);
int main(int argc, char *argv[])
{
int x = 3;
newvalue(&x);
printf("%i\n",x);
system("pause");
}
int newvalue(t)
{
int *t;
*t = 5;
return 0;
}
Nice try, but no.Code:int newvalue(t); int newvalue(t) { int *t; *t = 5; return 0; }
Function prototypes and declarations are designed to let the compiler know what the function expects. Look up function prototypes or look through any of the hundreds of examples on this site that create a function to see the correct way of going about it.
If you understand what you're doing, you're not learning anything.
Because your code does several things that don't make sense. Perhaps you could tell us what you're trying to do and we could show you a better example. You could then look at what's different and see where you went wrong.
Also, use code tags. Place [/code] and [code] and the end and beginning of your code, respectively (I did it in backwards order so that the code tags wouldn't format my explanation of how to use code tags. They make your code a lot easier to read.
Pretty cool, eh?Code:int main(int argc, char *argv[]) { int x = 3; newvalue(&x); printf("%i\n",x); system("pause"); } int newvalue(t) { int *t; *t = 5; return 0; }
ok, what i am trying to do is pass the address of a variable to a function, then change the variable within the fuction by using a pointer to the address of the variabel
see if this makes things clearer:
Code:int sum(int a, int b); // prototype int sum(int a, int b) { // implementation int result = a + b; return result; } int main(void) { printf("%d\n", sum(512, 512)); } int sum(int a, int b, int * result) // prototype int sum(int a, int b, int * result) { // implementation *result = a + b; return *result; } int main(void) { int result; sum(512, 512, &result); printf("%d\n", result); }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
just a few simple fixes
Code:#include <stdio.h> #include <stdlib.h> int newvalue(int *); int main(int argc, char *argv[]) { int x = 3; newvalue(&x); printf("%i\n",x); system("pause"); return 0; } int newvalue(int *t) { *t = 5; return 0; }
thanks sand_man, I am trying to learn to apply pointers without using the books examples, i see how I was thinking wrong now.