I have a larger piece of code (I didn't write it) that makes use of complex numbers defined through a structure. I need to edit it and simply multiply a complex number by a real but cant seem to get it right. I keep getting the following error message.
error: invalid operands to binary * (have ‘cplx’ and ‘double’)
I know this could be done using the complex.h library but that would mean a lot of rewriting so is there a simpler way? The code below reproduces my problem.
#include <stdio.h>
#include <math.h>
typedef struct cplxS {
double re;
double im;
} cplx;
int main()
{
double a = 1.3;
cplx b = {1, 2};
c = a * b;
}
You will first have to initialize a node using malloc
#include <stdlib.c>
int main(){
double a = 1.3;
//initialize struct
struct cplxS* b = malloc(sizeof(struct cplxS));
//set values for b
b->re = 1;
b->im = 2;
//preform your multiplication
double c = a*(b->re); //value c with re
double d = a*(b->im); //value d with im
//free node memory
free(b);
b = NULL;
}
If you want to update the struct by multiplying c to its values, it would follow
#include <stdlib.c>
int main(){
double a = 1.3;
//initialize struct
struct cplxS* b = malloc(sizeof(struct cplxS));
//set values for b
b->re = 1;
b->im = 2;
//preform your multiplication
b->re = a*(b->re); //update b with a*re
b->im = a*(b->im); //value b with a*im
//free node memory
free(b);
b = NULL;
}
Hope this helps!
You need to either cast the double as a complex number, and use the complex multiplication function (which presumably exists, or could/should be written by overloading * operator)
int main()
{
double a = 1.3;
cplx a_c = {1.3, 0}
cplx b = {1, 2};
c = a_c * b;
}
or actually perform the multiplication, either by building a definition for multiplication of reals and complex numbers (not shown), or just doing it yourself.
int main()
{
double a = 1.3;
cplx b = {1, 2};
cplx c = {b.re*a, b.im*a};
}
Related
I have an array of complex numbers and want to add a new complex number at the very first postion of the array.
I have a struct and a function which enters a new complex number at the first position of the array.
I have a struct which holds complex numbers
struct Complex
{
float imag;
float real;
};
I also have a function which is creating an instance of such a complex number on the heap
struct Complex* instanceOfComplex(float a, float b)
{
struct Complex* complex = malloc(sizeof(struct Complex));
if(complex == NULL)
{
return NULL; // out of memory
}
complex->imag = a;
complex->real = b;
return complex;
}
Finally I have a function which should place a new complex number at the very first position of an array of complex numbers
int attach(struct Complex *complexArray, struct Complex complex, int length)
{
int i;
for(i = length; length > 0; i--)
{
complexArray[i] = complexArray[i - 1];
}
complexArray[i] = complex;
length++;
return length;
}
The main function looks like this
int main()
{
float a = 3.6;
float b = 6.8;
struct Complex* complex = instanceOfComplex(a, b);
printf("%f %f\n", complex->imag, complex->real);
int length = 4;
struct Complex* complexArray = malloc(sizeof(length + 1) * sizeof(struct Complex));
complexArray[0] = *instanceOfComplex(8.2, 9.3);
complexArray[1] = *instanceOfComplex(7.1, 4.6);
complexArray[2] = *instanceOfComplex(0.1, 2.7);
complexArray[3] = *instanceOfComplex(5.6, 1.9);
attach(complexArray, *complex, length);
}
So finally the array should contain 5 elements of complex numbers where the new complex number is at position complexArray[0] and the other 4 elements are the following elements.
The problem is in the main function because all elements are holding (8.2, 9.3). Could someone please tell me, whats wrong here?
You are allocating memory for a new instance in instanceOfComplex and you return that (i.e. you return a pointer to the newly allocated memory).
In your main you dereference the pointer and assign the values to the array element.
You are loosing memory. Eiter allocate in main an array of pointers to complex, or have a local variable in instanceOfComplex that you return by value.
As user Yakov Dan says in his answer, you wrongly allocate the memory in main which probably causes your error.
When you allocate memory for your array, it's wrong to say sizeof(length+1).
Instead, that line should be:
struct Complex* complexArray = malloc((length + 1) * sizeof(struct Complex));
The following code gives me a Compile error "incompatible types at assignment"
File 1:
struct x{
int a;
int b;
int c;
};
File 2:
static struct x d;
void copyStructVal(){
d-> a = 1;
d-> b = 2;
d-> c = 3;
}
x getStruct(){
copyStructVal();
return d;
}
File 3:
static struct x e;
void copy(){
e = getStruct();
}
I've searched for this and can't find the answer. Can I do it with a Pointer? (I'm a amateur in C)
In C, you need to write struct behind the name of a structure, unless you typedef it. In other words:
x getStruct(){
Must be:
struct x getStruct(){
Since you wrote it in the rest of the code, I guess it is a typo.
On top of that, you have to fix these 3 lines, since d is not a pointer:
d-> a = 1;
d-> b = 2;
d-> c = 3;
They should be:
d.a = 1;
d.b = 2;
d.c = 3;
So I am now rewriting my fortran code in C (to use CUDA), and apparently I do not understand how to properly use malloc and pointers. I am trying to make the main function just calls to other functions, which need to malloc arrays that will then be used inside other functions. So, I am passing pointers of pointers to them as per this post: C Programming: malloc() inside another function
But the right amount of memory is not being allocated so I get segmentation faults. Here is the code:
#include <stdio.h>
#include <stdlib.h>
//#include <cuda.h>
#include <math.h>
//#include "cublas.h"
//datatype to match FORTRAN complex type
typedef float real;
typedef struct{
int nx;
int ny;
int nz;
int sz;
int tz;
} states;
void set_SPB(real **,int,states **,states **,int **);
//void set_SPB();
int find_minimum(int a[], int n,int start);
const real hc =197.32697,pi=3.1415927;
int main(){
int nmax = 2, A = 28;
real *etemp, *fock;
int *Ndex,*lookup,*lookup_a;
states *channel,*SPB;
//!generates the single particle basis to be used
set_SPB(&etemp,nmax,&SPB,&channel,&Ndex);
free(etemp);
free(Ndex);
free(SPB);
return 0;
}
void set_SPB(real **etemp,int nmax,states **SPB,states **channel,int **Ndex){
int tot_orbs = (2*nmax+1)*(2*nmax+1)*(2*nmax+1)*4;
int D = tot_orbs/4;
int Nalpha = (2*nmax+1)*(2*nmax+1)*(2*nmax+1)*9;
real E;
*etemp = (real*)malloc(D);
*Ndex = (int*)malloc(D*3);
*SPB = (states*)malloc(tot_orbs);
printf("orbits without spin degeneracy %d \n",D);
printf("size of etemp %ld \n",sizeof(*etemp)/sizeof(*etemp[0]));
return;
int i = 0;
for(int nx =-nmax;nx<=nmax;nx++){
for(int ny =-nmax;ny<=nmax;ny++){
for(int nz =-nmax;nz<=nmax;nz++){
E = 0.5*4.0*pi*pi*(nx*nx+ny*ny+nz*nz);
//printf("%d\n",i);
*etemp[i] = E;
*Ndex[0*D+i] =nx;
*Ndex[1*D+i] = ny;
*Ndex[2*D+i] = nz;
i+=1;
}
}
}
return;
}
Also I am not sure exactly if my assignments of the arrays are correct.
Specifically the print to find the number of elements of that have been allocated always gives 2, when it should be D = 125.
I cannot believe that float and int take only 1 byte in your environment.
Multiply the size to be allocated by size of their elements.
*etemp = malloc(sizeof(**etemp) * D);
*Ndex = malloc(sizeof(**Ndex) * D*3);
*SPB = malloc(sizeof(**SPB) * tot_orbs); /* not sure because this is not used */
Note that they say you shouldn't cast the result of malloc() in C.
Also note that [] operator has higher precedence than * operator, so you have to use parentheses to use the arrays.
(*etemp)[i] = E;
(*Ndex)[0*D+i] =nx;
(*Ndex)[1*D+i] = ny;
(*Ndex)[2*D+i] = nz;
Im having a small math library for 3d vector and Im trying to "unify" it.
Instead of having multiple typedef struct for vector3f, vector3i, color3, angles etc... Im trying to put everything inside the same struct like this:
typedef struct
{
union
{
float x;
float r;
float ax;
int x_int;
};
union
{
float y;
float g;
float ay;
int y_int;
};
union
{
float z;
float b;
float az;
int z_int;
};
} vec3;
Everything works peachy as long as the type is float, however when it falls to int Im having some strange values (which is understandable). My question is: Is there a way to cast directly/automatically inside the structure definition or I have to create extra functions to typecast between float and int?
Due to the answers below, maybe I should modify my original question to the following:
What is the best way to "unify" (and by unify I mean have like 1 struct) to be able to handle at the same time the following:
vector3f (float x,y,z)
vector3i (int x,y,z)
RGB (float r,g,b)
RGB (unsigned char r,g,b)
euler angle (ax, ay, az)
Thanks in advance!
If you mean that you want to put '360.0f' into float z of a union and have int z_int == 3, or vice versa, you can't. That is not the purpose of a union, and the binary representation of 3 (an integer) and 3.0 (a floating point value) are dissimiliar.
However, you could just remove the int and cast one of the floats to an int.
#include <stdio.h>
#include <stdlib.h>
typedef struct genericStruct
{
void *valueOne;
void *valueTwo;
}GS;
int main()
{
GS *gs = malloc(sizeof(*gs));
int valueInt = 10;
float valueFloat = 3.141592653589;
int *inputIntPtr = (int*)malloc(sizeof(int));
float *inputFloatPtr = (float*)malloc(sizeof(float));
void *voidPtr = NULL;
*inputIntPtr = valueInt;
*inputFloatPtr = valueFloat;
voidPtr = inputIntPtr;
gs->valueOne = voidPtr;
int *outputIntPtr = (int*)malloc(sizeof(int));
outputIntPtr = gs->valueOne;
printf("Input ptr = %d\n", *inputIntPtr);
printf("Output ptr = %d\n", *outputIntPtr);
voidPtr = inputFloatPtr;
gs->valueTwo = voidPtr;
float *outputFloatPtr = (float*)malloc(sizeof(float));
outputFloatPtr = gs->valueTwo;
printf("Input ptr = %f\n", *inputFloatPtr);
printf("output ptr = %f\n", *outputFloatPtr);
free(gs);
free(inputIntPtr);
free(inputFloatPtr);
free(outputIntPtr);
free(outputFloatPtr);
return 0;
}
And this what I meant by using void types.
This is a small piece of code that i wrote for you.It should do the job.I hope i was able to do what you asked for...
typedef struct{
void *ptr1;
void *ptr2;
void *ptr3;
}VEC;
main(){
VEC v ;
VEC *ptr;
int a = 5;
double b = 6;
float c = 7;
v.ptr1 = NULL;
v.ptr2 = NULL;
v.ptr3 = NULL;
ptr = &v;
v.ptr1 = (int *)&a;
ptr->ptr1 = (int *)&a;
v.ptr2 = (double *)&b;
ptr->ptr2 = (double *)&b;
v.ptr3 = (float *)&c;
ptr->ptr3 = (float *)&c;
printf("%d\n",*(int *)v.ptr1);
printf("%d\n",*(int *)(ptr->ptr1));
printf("%lf\n",*(double *)v.ptr2);
printf("%lf\n",*(double *)(ptr->ptr2));
printf("%f\n",*(float *)v.ptr3);
printf("%f\n",*(float *)(ptr->ptr3));
}
Or change all variables to void pointer type and then cast them to float or integer. Is it OK?
#define MAX 100
struct bs{
int ab;
int ac;
}be;
struct s{
be b;
int c;
int d;
int e;
}fe;
int a[MAX];
fe f;
Technique 1:
f.b.ab = a;
memset(&a,0,sizeof(a));
f.b.ac = MAX;
Technique 2:
f.b.ab = a;
f.b.ac = MAX;
memset(&a,0,sizeof(a));
Technique 3:
memset(&a,0,sizeof(a));
f.b.ab = a;
f.b.ac = MAX;
which is the best technique to follow and why?
Technique 3:
memset(&a,0,sizeof(a));
f.b.ab = a;
f.b.ac = MAX;
because both a and fb.b.ab will have clear memories with only one memset() call. Any other kind of optimization you might been hoping for is insignificant.
None of the above. The best would be:
int a[MAX] = { 0 };
fe f = { .b.ab = a, .b.ac = MAX };
None. What you probably mean for a start is memset(a,0,sizeof(a));
a is an array that is converted to a pointer in that context, no need to take the address. (The C FAQ has more information about arrays and pointers.)
Then for the rest of it you should revise your basic knowledge of C before you go further
struct s{
be b;
int c;
int d;
int e;
}fe;
fe f;
Here the structure type is struct s and fe is a variable of that type. So the last line here makes no sense at all.