0

Ok so I'm sure there's a simple fix that I'm missing, but right now my code is causing a segment fault on the line "A[i]->key = 0;." The Record* Item part is a necessity for the program, so I need to make it work this way for an assignment I'm working on, however if I do change it so that Item becomes a non-pointer typedef of Record, then I can use A[i].key no problem. I just need a nudge in the right direction so that I can make standInput correctly assign values to an array of pointers to records. Thanks!

Item.h:

#include "stdio.h"
#include "stdlib.h"

typedef int keyType;

struct Record
{
    keyType key;
    int other;
};
typedef struct Record* Item;

void standInput(Item *A, int n)
{
    int i, input;
    for(i = 0; i <= n-1; i++)
    {
        A[i]->key = 0;
        printf("%d ", A[i]->key);
    }
}

Main:

#include "stdio.h"
#include "stdlib.h"
#include "Item.h"

int main()
{
    int n;
    Item *A;
    printf("Enter a length for the array: ");
    scanf("%d", &n);
    A = (Item*)malloc(n * sizeof(Item));
    standInput(A, n);
    return 0;
}
1
  • 1
    are you sure A is of Item* and not just Item ? Commented Apr 22, 2012 at 22:30

4 Answers 4

2

The values in A are all uninitialized, but you're using them as struct Record pointers anyway. If you want to have A continue holding pointers (rather than the structs directly), then you need to allocate space for A and for each item pointed to by A.

Sign up to request clarification or add additional context in comments.

1 Comment

Hobbs: Thank you! I would never have gotten that, but it worked perfectly.
2

Note that Item is already a pointer!

You have to allocate space for the struct, not for the pointer:

A = (Item)malloc(n * sizeof(struct Record));

Note: If the typedef for pointer confuses you, don't use it ;)

A[i]->key means that A[i] is a pointer, but you just allocated an array, so use A[i].key.

Note: you have to change the type of A accordingly.

2nd solution: if you want A[i] to be a pointer, you have to fist allocate space for the pointers (as you do now), then for each pointer (in a loop) allocate space for the struct.

2 Comments

I'm not sure this is a problem; the standInput function accept a Item pointer and A is declared as Item*. Am i wrong?
It depends on how you want to tackle the problem. A continuous array of elements or an array of pointers?
0

Your structure name is Record not Item. So you should use sizeof(struct Record).

3 Comments

Thanks, I switched it out to use sizeof(struct Record), but it's still giving me the same problem.
I'm not sure i understand what you want your code to do. Are you creating an array of "Item"?
Yeah the assignment asked for an array of Items that are pointers to structs Record. Honestly I'm still a little confused by it as well since the instructors communication skills aren't the greatest, but I've been piecing it together from Robert Sedgewick's book Algorithms in C.
0

Do it this way:

int main()
{
    int n, i;
    Item *A;
    printf("Enter a length for the array: ");
    scanf("%d", &n);
    A = (Item*)malloc(n * sizeof(Item));
    for(i=0; i<n; i++){
        A[i] = (Item)malloc(sizeof(struct Record));
    }
    standInput(A, n);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.