typedef struct veri{
int k[9],v[10];
}v;
v a[27];
int h(int x){
return x%27;
}
int put (v data){
if(a[h(data.k)].k == -1){
a[h(data.k)] = data;
return 1;
}
else {
int donusSayisi =0;
int indis = h(data.k);
while(a[indis].k != data.k && a[indis].k!=-1){
//printf("%d %d %d \n",a[indis].k, data.k, indis);
indis ++;
if(indis>=27){
if(donusSayisi>=1){
printf("hashtable dolu\n");
return -1;
}
indis = indis %27;
donusSayisi++;
}
}
a[indis].v[indis]= data.v[indis];
a[indis].k[indis] = data.k[indis];
}
}
ı am trying create hash table with array .But ı cant add it .I cant put it and get it.Where is my mistake.I am getting crazy because of this errors.This not my all code just a part..
if(a[h(data.k)].k == -1){
passing argument 1 of 'h ' makes integer from pointer without a cast
comparison between pointer and integer
return a[indis].v;
return makes integer from pointer without cast
Related
I need to calculate the height of a trie tree in C
the node struct is as follows:
struct trie_cel {
char type; // 'I': internal / 'P': letter
struct trie_cel *child [HEIGHT_ALPHABET]; // use the function CHAR_TO_INDEX to get child node of each letter
};
typedef struct trie_cel no;
i'm trying to use recursion
if(r == NULL) return -1;
if(!r) return 0;
int alt = 0;
int heightM = 0;
no** i = r->child;
no** fim = i + (sizeof(r->child) / sizeof(no *));
while(i != end){
alt = height(r->child[i])+1;
if(alt > heightM){
heightM = alt;
}
}
return heightM;
However, my code is presented with the following problem, could anyone help me?
trie.cpp: In function ‘int altura(no*)’:
trie.cpp:146:32: error: invalid types ‘trie_cel* [27][no** {aka trie_cel**}]’ for array subscript
alt = height(r->child[i])+1;
^
chmod: cannot access 'vpl_execution': No such file or directory
I was able to calculate using "int" but i'd like to learn how to calculate using pointers
Maybe use the pointer directly 'height(*i)'. Or use pointer arithmetics to convert to integer type: 'i - r->child'
I believe this is how you should approach:
int trie_height(no *root) {
if (root == NULL) return -1;
int i, h = 0;
for (i = 0; i < HEIGHT_ALPHABET; i++) {
int res = 1 + trie_height(root->child[i]);
if (res > h)
h = res;
}
return h;
}
Since child is an array, you can't adress it's elements anything other than an integer. i isn't an integer in your code, so it wouldn't work. Also, you're doing nothing with fim after assigning it, so it's useless there.
I'm trying to do a implementation of a deque with dynamic allocation, but I'm having some trouble since the values of the variables in the struct are different out of the function initialize, and I dont know why.
By some reason, the program always end up with a segmentation fault in the push_front/back part.
// C program for vetor implementation of d
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int data;
// A structure to represent a d
typedef struct deque
{
int front, rear, size;
unsigned int capacidade;
data* vetor;
}deque;
// function to create a d of given capacidade.
// It initializes size of d as 0
void initialize(deque *d, unsigned int capacidade){
d = (deque*) malloc(sizeof(deque));
d->capacidade = capacidade;
d->front = 0;
d->size = 0;
d->rear = capacidade-1; // This is important, see the enqueue
d->vetor = (data*) malloc(d->capacidade * sizeof(data));
}
// deque is full when size becomes equal to the capacidade
int full(deque* d){
if(d->size == d->capacidade)
return 1;
else return 0;
}
// deque is empty when size is 0
int empty(deque* d){
return (d->size == 0);
}
// Function to add an item to the d.
// It changes rear and size
int push_back(deque* d, int item){
if (full(&d))
return 0;
d->rear = (d->rear + 1)%d->capacidade;
d->vetor[d->rear] = item;
d->size = d->size + 1;
}
int push_front(deque* d, int item){
if (full(&d))
return 0;
d->front = (d->front - 1+d->capacidade)%d->capacidade;
d->vetor[d->front] = item;
d->size = d->size + 1;
}
// Function to remove an item from d.
// It changes front and size
int pop_front(deque* d){
if (empty(&d))
return 0;
int item = d->vetor[d->front];
d->front = (d->front + 1)%d->capacidade;
d->size = d->size - 1;
return item;
}
int pop_back(deque* d){
if (empty(&d))
return 0;
int item = d->vetor[d->rear];
d->rear = (d->rear - 1+d->capacidade)%d->capacidade;
d->size = d->size - 1;
return item;
}
// Function to get front of d
int front(deque* d)
{
if (empty(d))
return 0;
return d->vetor[d->front];
}
// Function to get rear of d
int rear(deque* d)
{
if (empty(d))
return 0;
return d->vetor[d->rear];
}
// Driver program to test above functions./
int main()
{
deque* d;
int operacoes=0, tamdeque=0, i=0;
char opcao[100];
scanf("%d %d", &operacoes, &tamdeque);
initialize(&d, tamdeque);
while(i<=operacoes){
scanf("%s", opcao);
if(!strcmp(opcao, "insereI")){
if(full(&d)){
printf("cheia\n");
}
else{
data item;
scanf("%d", &item);
printf("%u\n", &d->capacidade);
push_front(&d, item);
}
}
else if(!strcmp(opcao,"insereF")){
if(full(&d)){
printf("cheia\n");
}
else{
data item;
scanf("%d", &item);
push_back(&d, item);
}
}
else if(!strcmp(opcao, "removeI")){
if(empty(&d)){
printf("vazia\n");
}
else{
pop_front(&d);
}
}
else if(!strcmp(opcao, "removeF")){
if (empty(&d)){
printf("vazia\n");
}
else{
pop_back(&d);
}
}
i++;
}
return 0;
}
Mis-match arguments
In many places, the following type of error. Readily findable will all warnings enabled.
warning: passing argument 1 of 'pop_back' from incompatible pointer type [-Wincompatible-pointer-types]
int push_back(deque* d, int item){
// if (full(&d))
if (full(d))
main()
// deque* d;
deque d;
initialize(&d, tamdeque);
mixing int/unsigned math.
Recommend a design change to use just one.
Missing return value
Minor: int push_back(), push_front()
Other problems exist. (About 30 total warnings)
Create a function with a static variable that is a pointer (with a default argument of zero). When the caller provides a value for this argument it is used to point at the beginning of an array of int. If you call the function with a zero argument (using the default argument), the function returns the next value in the array, until it sees a “-1” value in the array (to act as an end-of-array (indicator). Exercise this function in main( ).
Here is what I have :
int pr(int *p = 0) {
static int* po =0 ;
if (p) {
po = p;
return *po;
}
else {
return -1;
}
if (*p == -1) {
return -1;
}
return *po++;
}
int ar[] = {2,5,1,2,6,-1};
int main() {
pr(ar);
int pl;
pl = pr();
while (pl != -1) {
cout << pl << endl;
pl = pr();
}
}
When I start it, nothing gets printed and I dont know why. Any help ?
You will need to keep the next array index around, too:
int f(int* a = NULL) {
static int* arr = NULL; // internal state ...
static int idx = 0; // ...in these two vars
// Reset the internal state if a new array is given
if (a != NULL) {
arr = a;
idx = 0;
}
// #1
if (arr == NULL || arr[idx] == -1) { return -1; }
return arr[idx++];
}
I made some assumptions about the parts you didn't specify in your question on the line marked #1. If no pointer has yet been provided, or if the array end has previously been reached, we just return -1 each time.
In my binary search tree I want to create a function that can get all words starting with a prefix and store all words in an array called results
this is my tree
struct BinarySearchTree_t
{
char *mot,*def;
struct BinarySearchTree_t *left;
struct BinarySearchTree_t *right;
};
typedef struct BinarySearchTree_t BinarySearchTree;
my function :
size_t findWordsByPrefix(BinarySearchTree* tree, char* prefix, char*** results)
{
BinarySearchTree *tmp;
tmp=tree;
static int size=0;
if (!tmp)
return 0;
else if (strncmp(tmp->mot,prefix,strlen(prefix))==0)
{
(*results)= realloc(*results,(1+size)*sizeof(*(*results)));
(*(*results+size))= malloc(strlen(tmp->mot)*sizeof(char));
strcpy((*results)[size],tmp->mot);
size++;
return (1 + findWordsByPrefix(tmp->left,prefix, &results) + findWordsByPrefix(tmp->right,prefix, &results));
}
else
return (strncmp(tmp->mot,prefix,strlen(prefix))<0)?findWordsByPrefix(tmp->right,prefix, &results):findWordsByPrefix(tmp->left,prefix, &results) ;
}
This function should return a number of words starting with the given prefix.
my problem is that the program crash when it is run , and I don't how to resize my array results
so every time I found a word I should increase the size of the results array .
and I would know how exacly manipulate the pointer of pointer of pointer given in arg of this function (char ***results) : what exactly means?
If I simply compile your code, I get severe compiler warnings including:
1>binarysearchtree.c(98) : warning C4047: 'function' : 'char ***' differs in levels of indirection from 'char ****'
1>binarysearchtree.c(98) : warning C4024: 'findWordsByPrefix' : different types for formal and actual parameter 3
This alone will cause a crash -- you are calling your own function recursively with the wrong arguments.
Next, I believe you need to allocate one more than the length of the string, to hold a copy of a string:
malloc((strlen(tmp->mot) + 1 )*sizeof(char))
Next, you're passing around an array of strings of variable size -- and storing the size in a static variable. It's impossible to know if this will work, so don't do it.
Instead, if you want to use a dynamic array of strings, I suggest extracting out a struct to hold them, like so:
struct ResultTable_t
{
int size;
char **results;
};
typedef struct ResultTable_t ResultTable;
void InitializeResults(ResultTable *p_table)
{
p_table->size = 0;
p_table->results = NULL;
}
void AddResult(ResultTable *p_table, char *result)
{
if (result == NULL)
return;
p_table->size++;
p_table->results = realloc(p_table->results, p_table->size * sizeof(*p_table->results));
p_table->results[p_table->size-1] = malloc((strlen(result) + 1) * sizeof(**p_table->results));
strcpy(p_table->results[p_table->size-1], result);
}
void FreeResults(ResultTable *p_table)
{
if (p_table->results != NULL)
{
int i;
for (i = 0; i < p_table->size; i++)
{
free(p_table->results[i]);
}
free(p_table->results);
}
p_table->size = 0;
p_table->results = NULL;
}
(As an improvement, you might consider using geometric growth instead of linear growth for your table of results.)
Then your function becomes:
size_t findWordsByPrefix(BinarySearchTree* tree, char* prefix, ResultTable *p_table)
{
if (!tree)
return 0;
else if (strncmp(tree->mot,prefix,strlen(prefix))==0)
{
AddResult(p_table, tree->mot);
return (1 + findWordsByPrefix(tree->left,prefix, p_table) + findWordsByPrefix(tree->right,prefix, p_table));
}
else if (strncmp(tree->mot,prefix,strlen(prefix))<0)
{
return findWordsByPrefix(tree->right,prefix, p_table);
}
else
{
return findWordsByPrefix(tree->left,prefix, p_table);
}
}
And you would use it like:
ResultTable results;
InitializeResults(&results);
// Get some prefix to search for.
char prefix = GetSomePrefix();
int size = findWordsByPrefix(tree, prefix, &results);
// Do something with the results
// Free all memory of the results
FreeResults(&results);
Update
If the ResultTable is distasteful for some reason, you can pass the dynamic array and array sizes in directly:
void AddResult(char ***p_results, int *p_size, char *word)
{
if (word == NULL)
return;
(*p_size)++;
(*p_results) = realloc(*p_results, ((*p_size)+1) * sizeof(**p_results));
(*p_results)[(*p_size)-1] = malloc((strlen(word) + 1) * sizeof(***p_results));
strcpy((*p_results)[(*p_size)-1], word);
}
void FreeResults(char ***p_results, int *p_size)
{
int i;
if (p_results == NULL || *p_results == NULL)
return;
for (i = 0; i < (*p_size); i++)
{
free ((*p_results)[i]);
}
free (*p_results);
*p_results = NULL;
*p_size = 0;
}
size_t findWordsByPrefix(BinarySearchTree* tree, char* prefix, char ***p_results, int *p_size)
{
if (!tree)
return 0;
else if (strncmp(tree->mot,prefix,strlen(prefix))==0)
{
AddResult(p_results, p_size, tree->mot);
return (1 + findWordsByPrefix(tree->left,prefix, p_results, p_size) + findWordsByPrefix(tree->right,prefix, p_results, p_size));
}
else if (strncmp(tree->mot,prefix,strlen(prefix))<0)
{
return findWordsByPrefix(tree->right,prefix, p_results, p_size);
}
else
{
return findWordsByPrefix(tree->left,prefix, p_results, p_size);
}
}
and use like:
char **results = NULL;
int tablesize = 0;
// Get some prefix to search for.
char prefix = GetSomePrefix();
int size = findWordsByPrefix(tree, prefix, &results, &tablesize);
// Do something with the results
// Free all memory of the results
FreeResults(&results, &tablesize);
This function appends a desired value to the end of the array. When I tested the code, I used the value 100776, but when I printed out the array, the value of the last element was 135009, which is completely different than what my desired value was. Does anybody know why that's the case?
Here is the struct for my append function:
typedef struct {
int* data;
unsigned int len;
} intarr_t;
And this is my actual append function:
intarr_result_t intarr_push( intarr_t* ia, int val )
{
unsigned int len = ia->len;
if (ia == NULL)
{
return INTARR_BADARRAY;
}
else
{
ia->data = realloc(ia->data, (sizeof(int)*len+1));
if (ia->data != 0)
{
ia->data[len+1]=val;
ia->len=len+1;
assert (ia->data);
return INTARR_OK;
}
else
{
return INTARR_BADALLOC;
}
}
return 0;
}
given you have:-
(sizeof(int)*len+1)
then this is off the end of your array :-
ia->data[len+1]=val;
for two reasons :-
you haven't allocated enough memory because the + 1 occurs after the multiplication.
if you allocate 1 then your first spot is data[0] not data[1].
so you should do :-
a->data = realloc(ia->data, (sizeof(int)*(len+1)));
and
ia->data[len]=val;
ia->len++;
and your last item is ia->data[ia->len-1];