0

So, I've been trying to implement a dictionary using an array(still haven't written DiDelete function, not relevant right now), but two problems emerged, here's the code:

#include <stdio.h>
#include <stdlib.h>
#define MAX 1000

typedef int elementtype;

typedef struct{
    int last;
    elementtype elements[MAX];
}Dictionary;

void DiMakeNull(Dictionary *A)
{
    (*A).last = -1;
}

int DiMember(elementtype x, Dictionary A)
{
    int f,m,l;
    f = 0;
    l = A.last;
    m = (f+l)/2;
    while(f <= l )
    {
        if( A.elements[m] == x) return 1;
        else if( A.elements[m] < x) f = m+1;
        else l = m-1;

        m = (f+l)/2;
    }
    return 0;
}

void DiInsert(elementtype x, Dictionary *A)
{
    int i = 0,j;
    elementtype temp;
    while( ((*A).elements[i] < x) && ((*A).last >= i) )
    {
        i++;
    }
    for(j = i ; j <= (*A).last; j++)
    {
        (*A).elements[j+1] = (*A).elements[j];
    }

    (*A).elements[i] = x;
    (*A).last++;
}

int DiEmpty(Dictionary A)
{
    if(A.last == -1) return 1;
    return 0;
}

void DiPrint(Dictionary A)
{
    int i;
    printf("Dict:\n");
    for(i = 0; i <= A.last; i++)
        printf("%d\n",A.elements[i]);
    printf("End!");
}

Question 1: why does DiPrint not work? After giving a Dictionary with confirmed numbers in its' array of elements it prints out random numbers. It seems fairly simple, I don't understand what I've gotten wrong there!

Question 2: Why is it, that when a function is e.g.

void function(Dictionary *A)

can't I use the notation A->last, but insted I have to use (*A).last

Thanks in advance!

EDIT: main program looks like this:

#include <stdio.h>
#include <stdlib.h>
#include "atp_dictionary_pomocu_liste.c"

int main()
{
    Dictionary A;
    DiMakeNull(&A);
    DiInsert(4,&A);
    DiInsert(3,&A);
    DiInsert(32,&A);
    DiPrint(A);
    return 0;
}

2 Answers2

2

Well I will tell you how I debugged. I like my compiler so I compiled the code.

 error: could not convert '& A' from 'Dictionary*' to 'Dictionary'
     DiPrint(&A);
               ^

It said to me that I have type mismatch in DiPrint().

Then three changes and it worked.

void DiPrint(Dictionary* A)
{
    int i;
    printf("Dict:\n");
    for(i = 0; i <= (*A).last; i++)
        printf("%d\n",(*A).elements[i]);
    printf("End!");
}

And also in answer to your question why do we need to derefence it before using? Because we passed the address of the structure. Unless we dereference it , we won't get the struct instance.


From the comment I again had to rollback the code, we need to pass the structure instance directly and so we did

DiPrint(A);

and

void DiPrint(Dictionary A)
{
    int i;
    printf("Dict:\n");
    for(i = 0; i <= A.last; i++)
        printf("%d\n",A.elements[i]);
    printf("End!");
}

And this would work without those unary *'s because we are working directly on the structure instance.

Isn't A->last and (*A).last the same thing?

Yes they are. That's why when you call it DiPrint(&A) this function would also work.

void DiPrint(Dictionary* A)
{
    int i;
    printf("Dict:\n");
    for(i = 0; i <= A->last; i++)
        printf("%d\n",A->elements[i]);
    printf("End!");
}
Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56
0

You have an error in your code; in DiPrint(Dictionary A), your function expects type Dictionary but not type Dictionary *, you need to modify this calling function part in your code, so it would be:

DiPrint(A);

Not

DiPrint(&A);

Or another solution is modifying your function to accept a pointer instead, so it would be:

void DiPrint(Dictionary* A)
{
    int i;
    printf("Dict:\n");
    for(i = 0; i <= (*A).last; i++)
        printf("%d\n",(*A).elements[i]);
    printf("End!");
}
ndrwnaguib
  • 5,623
  • 3
  • 28
  • 51