I am trying to print out the information from pID pointer that is located in Objects.h header file. Whatever I try to do I get the "Exception thrown: read access violation. p was 0xCEDECEDF." error in Visual Studio 2017. In other words it is unable to read memory. This is the code part from main.cpp file. The error comes in from the For cycle where I am trying to print out the info. I have debugged the code with no success.
#include "stdio.h"
#include "DateTime.h"
#include "Objects.h"
#include "Headers.h"
#include "Structs.h"
#include "malloc.h"
#pragma warning ( disable : 4996 ) //Disables some errors that might occure
void PrintObjects(Object9 **ppStruct1);
//int InsertNewObject(HeaderA **ppStruct1, char *pNewID, int NewCode);
//Object9* RemoveExistingObject(HeaderA **pStruct1, char *pExistingID);
int main()
{
Object9 **pStruct1 = (Object9 **)GetStruct1(9, 35);
Object9 **p = pStruct1;
int i;
for (i = 0; i < 35; i++)
{
printf("%p\n", *(p + i));
printf("%s\n", (*(p + i))->pID); //This is the line where the error
//occures
}
if (pStruct1 == NULL)
printf("pStrcut on NULL\n");
else
printf("Ok\n");
PrintObjects(pStruct1);
return 0;
}
This is the part from the Objects.h file.
typedef struct ob9
{ // formatting string for printf is "%s %lu %02d %s %04d\n", the result is
for example "Abcde 100 01 Detsember 2010"
// or "Abcde Fghij 100 01 Detsember 2010"
char *pID;
unsigned long int Code;
Date3 *pDate3; // Declaration of Date3 is in file DateTime.h
struct ob9 *pNext;
} Object9;
The GetStruct1 function comes from the Structure.h header file. I don't know if this helps to determine the problem or not, but just in case I'm also adding the entire Structs.h file code. I need to use: "void **GetStruct1(int ObjectType, int nObjects);" function.
//
// Structures: function prototypes
// -------------------------------
void **GetStruct1(int ObjectType, int nObjects);
HeaderA *GetStruct2(int ObjectType, int nObjects);
HeaderB *GetStruct3(int ObjectType, int nObjects);
HeaderC *GetStruct4(int ObjectType, int nObjects);
HeaderA **GetStruct5(int ObjectType, int nObjects);
HeaderA *GetStruct6(int ObjectType, int nObjects);
HeaderD *GetStruct7(int ObjectType, int nObjects);
/* ObjectType - 1, 2,...10 - type of objects to be inserted into the data
structure.
nTypes -number of objects to be inserted into the data structure.
ObjectType and nObjects are specified by the instructor.
Return value - pointer to the first byte of data structure.
Examples for structure from 25 objects of type 4:
Object4 **pStruct1 = (Object4 **)GetStruct1(4, 25);
HeaderA *pStruct2 = GetStruct2(4, 25);
HeaderB *pStruct3 = GetStruct3(4, 25);
HeaderC *pStruct4 = GetStruct4(4, 25);
HeaderA **pStruct5 = GetStruct5(4, 25);
*/
I can print out the address but not the info I need. I know I am doing something wrong but don't know exactly what. Any help would be appreciated.
EDIT: Changed the code a bit.