Sign in to follow this  

extern array seg fault

This topic is 2848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This code compiles with no errors or warnings but when I run my one-line program I get a seg fault: Record.h
#ifndef RECORD_H_INCLUDED
#define RECORD_H_INCLUDED

struct SubrecordType{
	unsigned int name;
	char desc[24];
};

#ifndef IN_RECORD_CPP
extern const int numsubrectypes;
extern const SubrecordType * const subrecordtypes;
#endif


#endif // RECORD_H_INCLUDED


Record.cpp
#define IN_RECORD_CPP

#include "Record.h"

const int numsubrectypes = 2;

extern const SubrecordType subrecordtypes[numsubrectypes] = {
	{ 0                        ,   "Unknown Subrecord" },
	{ *((unsigned int *)"NAME"),   "Object ID"         }
};


main.cpp
#include <stdio.h>
#include "Record.h"

int main()
{
    printf("%i", subrecordtypes[0].name);
    return 0;
}


However! Changing extern const SubrecordType * const subrecordtypes; to extern const SubrecordType subrecordtypes[]; seems to have fixed my problem and all is well. I don't understand this. I grew up thinking [] was just a preallocated *const, and basically synonymous. I wracked my brain for hours today breaking my program into pieces trying to find this problem. I'm looking for some insight. HOW are these different? And if they are fundamentally different types then why didn't my compiler (GCC on Windows) even warn me about it? [Edited by - likeafox on February 25, 2010 11:36:02 AM]

Share this post


Link to post
Share on other sites
int [] x; means that x is an array of integers.
int * y; means that y is a pointer to an array of integers.

The compiler will automatically convert when you use an array where you need a pointer. This means y=x is a valid statement, even though the types differ.

Share this post


Link to post
Share on other sites

This topic is 2848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this