[Solved] why pointer variable with asterisk and without asterisk behave differently in printf?

When you declare char string[]=”Hello” you declare an array of characters. string points to the first element of the array. * is known as the dereferencing operator. Suppose ptr is an integer pointer. *ptr will give you the content of the memory location pointed by the pointer ptr. At the time of declaration you have … Read more

[Solved] C++ Returning pointer from function

Yes they are exactly the same. The first code in words: SomeClass pointer sc = new SomeClass(); return SomeClass pointer sc The second code in words SomeClass pointer sc = new SomeClass(); SomeClass pointer rSc = SomeClass pointer sc return SomeClass pointer rSc As you can read/ see for yourself there is no difference since … Read more

[Solved] Basic C pointer syntax [closed]

* is both a binary and a unary operator in C and it means different things in different context. Based on the code you have provided: *leaf = (struct node*) malloc( sizeof( struct node ) ); Here the void * (void pointer) that malloc returns is being casted to a pointer to struct node, I … Read more

[Solved] Returning pointer to array doesn’t give expected output in c [duplicate]

newarray is an automatic local variable. It will no longer exists once function return. Returning pointer to an automatic local variable invoke undefined behaviour and in this case nothing good can be expected. You can allocate memory diynamically and then return pointer to it int *newArray = malloc(sizeof(int)*size); 2 solved Returning pointer to array doesn’t … Read more

[Solved] Segmentation fault when calling a function returning a pointer

Let’s use std::vector: #include <iostream> #include <vector> #include <iomanip> typedef std::vector<double> DoubleArray; DoubleArray v_scalar_prod(double a, double *b, int n) { DoubleArray res(n); for (int i = 0; i < n; i++) { res[i] = a*b[i]; std::cout << std::setprecision(10) << “res = ” << res[i] << ‘\n’; } return res; } int main() { double y[3] … Read more

[Solved] Segmentation fault using scanf() [closed]

Three problems here. First scanf(ifp,”%lf %lf\n”,&theta,&Cla); You’re calling scanf when you look like you want to use fscanf: fscanf(ifp,”%lf %lf\n”,&theta,&Cla); Second is the file you’re opening: ifp=fopen(argv[0],”r”); argv[0] is the name of the running executable, which is probably not what you want. If you want the first argument passed in, use argv[1] ifp=fopen(argv[1],”r”); Last is … Read more

[Solved] No More Confusing Pointers

Point 1: Nested functions are not standard C. They are supported as GCC extension.. Point 2: printf(“&b is:%s\n”,&b); is wrong and invokes UB, because of improper format specifier. You need to change that to printf(“&b is:%p\n”,(void *)&b); Point 3: &(&a) is wrong. the operand for & needs to be an lvalue, not another address, which … Read more