Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I create a Queue that includes a two dimensional array, the size of every element of that array is 2. An Exception occurs when I enqueue a 2-size array in the queue.

The following is my code:

#include "stdio.h"

#define size 1000

typedef struct Queue {
    int *data[2];
    int front;
    int rear;
}Queue;

void init(Queue *q)
{
    q->front=0;
    q->rear=0;
}

void Enqueue(Queue *q,int *value)
{
    if(q->rear==size)
        return ;

    q->data[q->rear++]=value;
}

void main()
{
    Queue q[1];

    init(q);

    int a[10][2];

    for(int i=0;i<10;i++) {
        a[i][0]=i;
        a[i][1]=i*2+1;      
        Enqueue(q,a[i]);    
    }   
}
share|improve this question
I very much doubt you're getting an exception in C, but please add exactly what error you're getting. Similarly, what happens if you run this in a debugger? – Philip Kendall 14 hours ago
you mean int data[size]; ? – Exceptyon 14 hours ago
The error out put is: 'Queue_LinkList_Stack.exe': Loaded 'C:\Windows\SysWOW64\winspool.drv', Cannot find or open the PDB file 'Queue_LinkList_Stack.exe': Loaded 'C:\Windows\SysWOW64\f_pbrc09.dll', Binary was not built with debug information. 'Queue_LinkList_Stack.exe': Loaded 'C:\Windows\SysWOW64\dwmapi.dll', Cannot find or open the PDB file First-chance exception at 0x013c1418 in Queue_LinkList_Stack.exe: 0xC0000005: Access violation writing location 0x0112ea00. Unhandled exception at 0x013c1418 in Queue_LinkList_Stack.exe: – GreenField26 14 hours ago

3 Answers

 *I create a Queue that includes a two dimensional array*

No you just create a pointer array in onw dimensional by int *data[2];

If you want create a Quene that include a two demensional array , it will be int data[size][size_anoter]

However, you didn't need I two dimensional . In void Enqueue(Queue *q,int *value) function , you pass the address from a[i] to data[i] . So you only need a big enough pointer array ;\

int * data[size];

Besides , It is better if you can define size as SIZE.

share|improve this answer

probably, Feel like the following

#include <stdio.h>
#include <stdlib.h>

#define size 1000

typedef struct Queue {
    int (**data)[2];
    int front;
    int rear;
} Queue;

void init(Queue *q){
    q->front=0;
    q->rear=0;
    q->data = malloc(size*sizeof(int (*)[2]));
}

void Enqueue(Queue *q,int (*value)[2]){
    if(q->rear==size)
        return ;

    q->data[q->rear++]=value;
}

int main(){
    Queue q[1];

    init(q);

    int a[10][2];

    for(int i=0;i<10;i++) {
        a[i][0]=i;
        a[i][1]=i*2+1;
        Enqueue(q, &a[i]);
    }
    //printf("%d\n", (*q->data[0])[1]);
    return 0;
}
share|improve this answer

I can see at least a couple of issues:

typedef struct Queue {
   int *data[2];

This is not a pointer to a two-dimensional array of ints, which I assume was what was intended. It is an array of two int pointers. For a 2D array of 1000 entries of arrays with two elements, you probably wanted:

typedef struct Queue {
   int data[size][2];

Then, when you call Enqueue(), copy in the array of two ints into the appropriate entry indicated by rear:

void Enqueue(Queue *q,int value[2])
{
    if(q->rear==size)
        return ;

    memcpy(q->data[q->rear], value, sizeof(q->data[q->rear]));
    q->rear++;
}

This solution will make a fixed array. If you want the array to be built dynamically it is a little more complicated as you need to malloc/calloc the memory and then free it when done.

share|improve this answer
it is int* not int – Lidong Guo 13 hours ago
@LidongGuo In the call to Enqueue()? Right you are, I misread that. – Daniel 13 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.