0
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <math.h>


void* findSin(void *arrayTwoD)
{
    float **myArray = ((float **)arrayTwoD);

    float total = 0.0;

    int x = 0;
    float *radAngle;


    for (int data = 0; data < 2; data++)
    {

        *radAngle = *(*(myArray + data) + 0); <---This is triggering the error


        for (int i = 1; i < 15; i += 2)
        {
            total += pow(*radAngle, i) / findFactorial(i);
        }

        printf("\nSin %f is %f \n", *myArray[data], total);
    }

    pthread_exit(0);
}



int main()
{
    pthread_t threadid1;
    float angleDegree;
    float angleRadian;


    float myArray[2][2];

    for (int i = 0; i < 2; i++)
    {
        printf("Please enter the angle in degree: ");
        scanf("%f", &angleDegree);

        angleRadian = (angleDegree / 180.0) * M_PI;
        myArray[i][0] = angleRadian;
    }

    pthread_create(&threadid1, NULL, findSin, (void *)myArray);

    pthread_join(threadid1, NULL);
}

**Please help me, i'm stuck trying to figure out how to correctly access the elements.Thanks.

Situation: The first column of the 2d array contains angles entered by the user. The function findSin uses the first column to find the sin value of the related element. I'm not able to access the element of the first column**

ERROR: Segmentation fault (core dumped)

jwdonahue
  • 6,199
  • 2
  • 21
  • 43
  • First problem: `radAngle` is a `float*`. This means that you're not actually allocating space for the float (just a pointer). You want it to a plain `float`. The other problem is you can't easily convert a `float**` to a `float[2][2]`; Multidimensional arrays and pointers-to-pointers are very different things. see https://stackoverflow.com/questions/1584100/converting-multidimensional-arrays-to-pointers-in-c – cajomar Feb 06 '21 at 18:54
  • A true 2-dimensional array of the form `float[][]` is ***not*** the same as a `float **` and you can not refer to a `float[][]` with a `float **`. Anyone who refers to a `float **` as an "2-dimensional array" is, at best, being inaccurate. A `float **` usually refers to a **one**-dimensional array of pointers to multiple, individual **one**-dimensional arrays of `float` values. – Andrew Henle Feb 06 '21 at 19:06

2 Answers2

0

Something is wrong with your address arithmetic. Here's a better approach that lets you just use your 2D array.

void * findSin(void *arrayTwoD){ 
    float (* myArray)[2] = arrayTwoD;
    float total=0.0;
    int x=0;
    float radAngle;

    for(int data=0; data<2; data++)
        {
            radAngle = myArray[data][0]; 
            for(int i=1; i<15;i+=2){
                total+=pow(radAngle,i)/findFactorial(i);
            }
            printf("\nSin %f is %f \n",*myArray[data],total);
        }
    pthread_exit(0);
}
stark
  • 12,615
  • 3
  • 33
  • 50
0

float **foo is not a pointer to a 2d array. It is a pointer to a pointer to a float.

To look at how things work in a 1 dimensional case:

/* a one-dimensional array */
float a[2];

/* A pointer to the first element in a sequence of 2 floats */
float *ap = a;

To extend this to the 2 dimensional case we, don't have sequence of float, but a sequence of array of length 2. If we use a typedef, we can make it similar to the 1d-case:

typedef float a2[2];

/* An array of an array, AKA 2d-array. */
a2 a[2];

/* A pointer to the first 1-d array in a sequence of 1-d arrays */
a2 *ap = a;

To get rid of the typedef we can write

/* An array of an array, AKA 2d-array. */
float a[2][2];

/* A pointer to the first 1-d array in a sequence of 1-d arrays */
float (*ap)[2] = a;

Be aware of the (..) in the above definition. You can think of the definition as if the expression (*ap)[n] is supposed to give a float. An since [] has higher precedence than * we have to use ().

Naturally you can do this with a void-pointer as intermediate:

float a[2][2];

void *vp = a;

float (*ap)[2] = vp;

Oh, and you access the elements in a through ap like in the 1-dimensional case: a[i][j] == ap[i][j]

HAL9000
  • 2,138
  • 1
  • 9
  • 20