#weird error cannot acces memory

14 messages · Page 1 of 1 (latest)

sullen flint
#

here is my code


void init_emps() {
    emp_count = 3;
    emps = (emp*)malloc(sizeof(emp) * emp_count);

    if (emps == NULL) {
        printf("could not malloc emps array");
        return 0;
    }
    else {
        add_emp();
        print_emps();
    }
}

void add_emp() {
    if (emps != NULL) {
        //emp_count++;
        //emps = realloc(emps, sizeof(emp) * emp_count);
        emp em = { "doon", 18 };
        emps[emp_count-1] = em;
    }
}

void print_emps() {
    for (int i = 0; i < emp_count-1; i++) {
        puts(emps[i].name);
    }
}``` i get this error ```Exception thrown at 0x00007FF87F337CA0 (ucrtbased.dll) in bankSystem.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.``` i get it when i try to print my emps does anyone know how to fix this?
proper sirenBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

untold mural
sullen flint
sullen flint
# daring oracle What is `library.h`?

here is my library.h ```#pragma once
#include <stdio.h>
#include <stdlib.h>

typedef struct {
char* name;
int age;
} emp;

emp* emps;

int emp_count;

void init_emps();
void add_emp();
void print_emps();```

untold mural
#

What are you trying to do on this project? Maybe I can help you further

#

@sullen flint

sullen flint
#

@untold mural

#

as a learning thing

untold mural
sullen flint
#

k