why I am trying to dynamically allocate memory for this pointer to array of structure
I have STUDENT *studArr[]
where STUDENT is the name of the structure
I tried to allocate like this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"
#include "stack.h"

#define NAME_SIZE 20
#define RECORDS_SIZE 100

typedef struct STUDENT
{
    char studName[20];
    int timeEnter;
    int timeUpdate;
}STUDENT;

typedef struct TUTOR
{
    char tutorName[NAME_SIZE];
    int tutorTime;
    STUDENT *ptr;
}TUTOR;

QUEUE *queue1;
STACK *stack1;

void getData(STUDENT *studArr[]);

int main(void)
{

    STUDENT *studArr[100];
    getData(studArr);








    return 0;
}

void getData(STUDENT *studArr[])
{
    FILE *fp;
    char fileName[NAME_SIZE];
    int first; 
    int i;
    printf("Enter the file name: ");
    gets(fileName);
    fp = fopen(fileName, "r");
    if (fp == NULL)
    {
       printf("Error opening file!\n");
       return;
    }
    fscanf(fp, "%d", &first);
     **studArr = malloc(sizeof(STUDENT*)*first);


}

But I got an error saying no operator "=" matches these operands

why am I getting this error and how can I fix the error?
thank in advance

Edited 3 Years Ago by bluekb

Would you maybe post the full code, if it's not too long? Also, when you use 2 dereferencing operators, you are accessing each individual sub-array index so doing sizeof(STUDENT*)*first would be a logic error.

I find the error new to me. Are you perhaps using a C++ compiler/IDE?

yes I'm using visual studio, and I will post the full code

the code STUDENT *studArr[] means an array of pointers of type STUDENT. *studArr means you are accessing the first index of the array of pointers and is essentially the same as studArr[0], which is of type STUDENT *. Therefore, what the code **studArr means is you are accessing the STUDENT instance being referred to by studArr[0]. But you are assigning a type pointer to a type STUDENT, therefore the error.

In order to check this, you can try to create a simple STUDENT instance in the function and assign it to **studArr and see that it will not produce a (compile) error.

In order for the particular line to not produce a compile error, you can simply remove the extra dereference operator so that *studArr = (STUDENT *) malloc(sizeof(STUDENT*)); and put a cast also since you're using a C++ compiler.

Edited 3 Years Ago by asrockw7: added

Legibilty helps a lot when you are trying to debug any program. I would therefore recommend typedef-ing your STUDENT structure to define a pointer to STUDENT as well. In practice I would do something like this:

typdef struct mystruct_s mystruct_t, *mystruct_p_t; //_t indicates a new type, _p_t indicates a pointer to a new type
struct mystruct_s //_s indicates a structure type
{
<members here>
};

That way, you wouldn't have to remember putting asterixes wherever they are needed, such as when allocating memory:

mystruct_p_t pStudent = NULL;
pStudent = (mystruct_p_t)malloc(sizeof(mystruct_t)); //Allocate enough memory for 1 mystruct_t
if (pStudent != NULL)
{
//do whatever you need with pStudent
free(pStudent);
pStudent = NULL;
}
This article has been dead for over six months. Start a new discussion instead.