[英]How do I initialize a double pointer in C? (array of pointers)
下面是我的代碼。 我收到一個錯誤,因為在分配 memory 給它之后,我沒有初始化總線 - > BusStops,一個指針數組。 要添加到總線的每個單獨的 BusStop -> BusStops 數組將在 AddBusStop 方法中分配給它的 memory。
但是,最初,bus->BusStops 在其中。 如何在考慮到這一點的情況下初始化總線 - > BusStops 數組,以免收到此錯誤?
我收到一個未初始化的值是由 Valgrind 上的堆分配錯誤和分段錯誤創建的
編輯 - 我嘗試在我的 InitializeBus 方法中使用 calloc 而不是 malloc ,但是唉,沒有運氣
//ONE METHOD I WROTE
void InitializeBus(Bus *bus, int maxBusStops) {
bus -> numBusStops = 0; //initializing numBusStops element from struct to 0
bus -> maxBusStops = maxBusStops; //initializing maxBusStops element from struct to parameter maxBusStops
bus -> BusStops = (BusStop**)malloc(maxBusStops * sizeof(BusStop*)); //add memory to bus -> BusStops
}
//THE OTHER METHOD I WROTE
int AddBusStop(Bus *bus, const char* name) { //NOTE - the bus variable passed in is already initialized from the previous method. "name" is the name of the bus stop.
for(int i = 0; i < bus -> numBusStops; i++) { //for each bus stop that is currently in the array
if(strcmp(bus -> busStops[i] -> name, name) == 0) { //if that bus stop has the same name as the bus stop passed in as a parameter
return 1; //return 1
}
//ERROR ALERT - READ BELOW
// ***I am getting a segmentation fault error at this line. An uninitialized value was created using heap allocation** in the final line of the InitializeBus method. How do I initialize bus -> BusStops once I have added memory to it? Each individual BusStop that is being added has memory allocated to it in this method, but the bus -> busStops array is uninitialized and I am not sure what to initialize it to
} //OTHERWISE
BusStop *newBus = (BusStop*)malloc(sizeof(BusStop) + 1); //create a new BusStop pointer to be added to the array of BusStop pointers, bu. I am not sure if I am allocating enough memory
newBus -> name = (char*)name; //set the name of this newBus bus stop equal to the name passed in as 0
newBus -> numDestinations = 0; //initialize numDestinations to 0
newBus -> destinations = NULL; //set newBus -> destinations to NULL - I was specified to do this and can't change it
if(bus -> numBusStops == bus -> maxBusStops) { //if there is no more space in bu to add the newly created bus
bu = (BusStop**)realloc(bu, bus -> maxBusStops * 2 * sizeof(BusStop)); //allocate double the memory to bu
bus -> maxBusStops *= 2; //make sure maxBusStops is now doubled. I am not sure if I am allocating enough memory
}
bus->numBusStops++; //increase the amount of bus stops the bus can hold by 1
bu[bus -> numBusStops] = newBus; //add the newly created bus at the end of the array bu. **THIS LINE IS WHERE I AM GETTING THE "Invalid Write of Size 8" error
bus->busStops = bu; //update the struct element bus -> busStops and set it equal to the updated bu value
return 0; //return 0
}
//MAIN METHOD TO TEST CODE
int main() {
Bus bus; //create a new bus
InitializeBus(&bus, 10); //pass in its address and a maxBusStop value of 10
const char *london = "London"; //create a new char pointer called london
AddBusStop(&bus, london); //add the London Bus Stop to bus -> busStop
const char *paris = "Paris"; //create a new char pointer called Paris
AddBusStop(&bus, paris); //add the Paris Bus Stop to bus -> busStop
}
//STRUCTS
typedef struct Bus {
BusStop** BusStops; /*The array of all Bus stops managed by the bus -- this is an array of pointers, each with type BusStop*/
int numBusStops; /*The current number of bus stops managed by the company*/
int maxBusStops; /*The max number of bus stops the BusStop array can store*/
} Bus;
typedef struct BusStop{
char \* name; /\*The bus stop name\*/
int numDestinations; /\*The number of destinations the bus stop offers rides to\*/
struct BusStop \*\* destinations; /\*The array of destinations the bus stop offers rides to\*/
} BusStop;
首先,你初始化總線了嗎? 如果不對其進行初始化,您將無法獲取其字段。 第二:檢查 BusStops 是否屬於 BusStop** 類型。 第三:因為 BusStop 是一個指針數組,如果你想在其中放入字符串 - 你需要使用 for 循環分配每一列 allocate BusStops[0], BusStops[1]...) 然后你可以放入字符串。 如果不分配列 - 當您嘗試比較字符串時,您的程序將無法工作 - 程序將 go 到 BusStops[i] 但不會分配字符串/內存。
我想到了!
我的問題出在 AddBusStop。 在增加 numBusStops 之后,我將 BusStop 添加到數組中。 由於 arrays 從索引 0 開始,這是錯誤的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.