Array of Struct Pointers (C Programming) Issues - c

So i'm trying to figure out how to do a few different things and I haven't worked with C that much, so any help would be much appreciated.
typedef int data_t;
typedef struct set {
data_t *array;
size_t capacity;
size_t size;
} set_t;
typedef data_t* set_i_t;
#define CLEAR -1
I have gotten this method working which uses malloc and allocates memory:
int set_init( set_t *set, int capacity ){
set->array = (data_t*)malloc(capacity * sizeof(data_t));
if(set->array == NULL){
return 1;
}
else{
set->capacity = capacity;
set->size = 0;
return 0;
}
}
And a method which frees it:
void set_free( set_t *set ){
free(set->array);
set->array = NULL;
set->capacity = set->size = 0;
}
In a separate method i'm trying to set all the values in the set to -1 (CLEAR)
void set_clear( set_t *set){
int i = 0;
for (i = 0; i < set->size; i++){
set->array = CLEAR;
}
set->size = 0;
}
Return the Size of the set:
int set_size( set_t set ) {
return sizeof(set->array);
}
Return the capacity:
int set_capacity( set_t set ) {
int capacity = set->capacity;
return capacity;
}
And then print the set:
void set_print( set_t set ) {
//Honestly don't feel like i'm ready for this one yet.
}
If anyone could walk me through a couple of these or give me a little assistance on how these can work, that would be awesome. Thanks guys!

A good resource is C dynamically growing array
1
You can read about size_t. What is size_t in C?
typedef int data_t;
// Here you are redefining int to data_t this is then used in array.
typedef struct set {
data_t *array;
// The address on heap where the typedef data_t is stored
size_t capacity;
size_t size;
} set_t;
typedef data_t* set_i_t;
// not sure why this is here maybe you use somewhere else
#define CLEAR -1
2
set_free( set_t *set); Looks good to me.
set_init(); yes but no
set_t set_init(int capacity) {
// create it here then return it.
set_t ret;
ret.array = (data_t*)malloc(capacity * sizeof(data_t));
if (ret.array == NULL) return NULL;
ret.capacity = capacity;
ret.size = 0;
return ret;
}
In the calling function
set_t A = set_init(5);
if (A == NULL) fprintf(stderr, "could not alloc memory\n");
// :)
3
void set_clear( set_t *set){
// you pass the address of the struct into the function. you could also use set_i_t
//int i = 0;
// why do this you can do it in the for loop as you can see
// for (i = 0; i < set->size; i++){
for (int i = 0; i < set->size; i++){
//set->array = CLEAR; common mistake
// you are saying the address of the array. aka array[0]
// this is the same as set->(array+i)
set->array[i] = CLEAR;
}
set->size = 0;
}
4 & 5
// looks good but again better ways of doing this.
set_size( set_t set );
set_capacity( set_t set );
Better ways of managing memory such as in the example here. C dynamically growing array
6
read all about printf();
http://www.tutorialspoint.com/c_standard_library/c_function_printf.htm
void set_print( set_t set ) {
// Here you passed the struct in plain and simple no pointer......
// so you will use the '.' not the '->'
// Here we can take a look at printf();
// %d is used to print int variables.
// to start off you know you will have to loop through the array.
for (int i = 0; i < set.size; i++) {
// you know the array must be at least have one item in it.
printf("%d\n", set.array[i]);
// using printf print the data_t aka "int" item in the array
}
}
Hope this helps. G

There were a few places where you defined the function arguments with set_t instead of set_t *.
Your set_size would just return the size of the array pointer (i.e. always 4 or 8), so that needed set->size
Also, set_clear was incorrect [and wouldn't even compile].
I've added some functions and implemented the [dreaded :-)] print function. No worries ...
Anyway, here's the corrected code [please pardon the gratuitous style cleanup]:
#include <stdio.h>
#include <malloc.h>
typedef int data_t;
typedef struct set {
data_t *array; // pointer to set's data
size_t capacity; // total number of data slots
size_t size; // number of slots currently in use
} set_t;
typedef data_t *set_i_t;
#define CLEAR -1
int
set_init(set_t *set, int capacity)
{
set->array = (data_t *) malloc(capacity * sizeof(data_t));
if (set->array == NULL) {
return 1;
}
else {
set->capacity = capacity;
set->size = 0;
return 0;
}
}
// And a method which frees it:
void
set_free(set_t *set)
{
free(set->array);
set->array = NULL;
set->capacity = set->size = 0;
}
// i'm trying to set all the values in the set to -1 (CLEAR)
void
set_clear(set_t *set)
{
int i = 0;
for (i = 0; i < set->size; i++) {
#if 0
set->array = CLEAR;
#else
set->array[i] = CLEAR;
#endif
}
set->size = 0;
}
// Return the Size of the set:
int
set_size(set_t *set)
{
return set->size;
}
// Return the maximum capacity:
int
set_capacity_max(set_t *set)
{
int capacity = set->capacity;
return capacity;
}
// Return the remaining available capacity:
int
set_capacity_avail(set_t *set)
{
int capacity = set->capacity - set->size;
return capacity;
}
// add some data
void
set_append(set_t *set,int val)
{
// NOTES:
// (1) this does _not_ check for overflow against capacity
// (2) when out of capacity, we might increase capacity and do a realloc
// on array
#if 0
if ((set->size + 1) >= set->capacity) {
set->capacity += 100;
set->array = realloc(set->array,sizeof(data_t) * set->capacity);
}
#endif
set->array[set->size++] = val;
}
// And then print the set:
void
set_print(set_t *set)
{
int i;
int len;
// Honestly don't feel like i'm ready for this one yet.
// Relax, no worries ...
len = 0;
for (i = 0; i < set->size; i++) {
len += printf(" %d",set->array[i]);
if (len >= 72) {
printf("\n");
len = 0;
}
}
if (len > 0)
printf("\n");
}
int
main(void)
{
set_t myset;
set_init(&myset,100);
set_append(&myset,17);
set_append(&myset,23);
set_append(&myset,37);
set_print(&myset);
set_free(&myset);
return 0;
}

Related

invalid use of incomplete typedef in C

I'm implementing a data structure in C and I get this error in my test file. Without adding code because then that would be a huge post with a ton of code to go through, but here's what my code looks like:
header.h file:
typedef struct array Arr;
functions.c file:
#include "header.h"
struct array{
int number;
int size;
char *names;
}
main.c file:
#include "header.h"
bool function(const Arr *const variable)
{
for (int i = 0; i < variable->size; i++)
{
variable->number[i] = i;
}
}
and so I get the error mentioned in the title referring to Arr*->number and Arr->*size. What I suspect to be the issue is that Arr is only typedefed but not defined. If that's the case, how can I resolve it?
Here's the main code:
main.c
#include <stdio.h>
#include "header.h"
int main(){
set *setA = set_empty();
set_insert(69,setA );
set_insert(15, setA);
set *setB = set_empty();
set_insert(12,setB );
set_insert(15, setB);
set *setDiff = set_difference(setA, setB);
printf("\n");
print_set(setDiff);
bool diff = verify_difference(setDiff, setA, setB);
}
bool verify_difference(const set *const setDiff, const set *const setA, const struct set *const setB)
{
bool answer = true;
for (int x = 0; x < setDiff->size; x++)
{
if (set_member_of(setDiff->array[x], setA) && set_member_of(setDiff->array[x], setB))
{
answer = false;
break;
}
}
return answer;
}
header.h
#ifndef HEADER_H
#define HEADER_H
#include <stdbool.h>
typedef struct set set;
set *set_empty();
void set_insert(const int value, set *s);
bool set_member_of(const int value, const set *const s);
functions.c
#include <stdio.h>
#include "header.h"
struct set {
int capacity;
int size;
char *array;
};
set *set_empty()
{
struct set *ptr = malloc(sizeof(struct set));
ptr->size = 0;
ptr->array = malloc(sizeof(char));
ptr->capacity = 1;
return ptr;
}
void set_insert(const int value, set *s)
{
if (!set_member_of(value, s)) {
int bit_in_array = value; // To make the code easier to read
// Increase the capacity if necessary
if (bit_in_array >= s->capacity) {
int no_of_bytes = bit_in_array / 8 + 1;
s->array = realloc(s->array, no_of_bytes);
for (int i = s->capacity / 8 ; i < no_of_bytes ; i++) {
s->array[i] = 0;
}
s->capacity = no_of_bytes * 8;
}
// Set the bit
int byte_no = bit_in_array / 8;
int bit = 7 - bit_in_array % 8;
s->array[byte_no] = s->array[byte_no] | 1 << bit;
s->size++;
}
}
set *set_difference(const set *const s1, const set *const s2)
{
struct set *s = set_empty();
for (int i = 0; i < s1->size; i++)
{
if (!set_member_of(s1->array[i], s2))
{
set_insert(s1->array[i], s);
}
}
for (int i = 0; i < s2->size; i++)
{
if (!set_member_of(s2->array[i], s1))
{
set_insert(s2->array[i], s);
}
}
return s;
}
bool set_member_of(const int value, const set *const s)
{
int bit_in_array = value;
if (bit_in_array >= s->capacity) {
return false;
}
int byte_no = bit_in_array / 8;
int bit = 7 - bit_in_array % 8;
char the_byte = s->array[byte_no];
return the_byte & 1 << bit;
}
The definition of the structure shall be available in main. Otherwise the compiler does not know whether there is the data member number in the structure referred in this statement
Arr->number[i] = i;
Moreover in any case this statement is incorrect because Arr is a type specifier and according to the structure definition the data member number is not an array
It seems you mean
variable[i].number = i;
But as the function parameter
bool function(const Arr *const variable)
is declared as a pointer to a constant object then you may not change pointed to data members of the structure.
So either move the definition of the function function from main.c in functions.c or place the structure definition in the header file.
And there is a typo
Typedef struct array Arr;
^^T
you need to use lower case letter
typedef struct array Arr;
I can only hazard a guess. Your code snippet could be wrong.
Move the structure definition to header.h & check.
//header.h file:
typedef struct array Arr;
struct array{
int number;
int size;
char *names;
};

how to do dynamic allocation in boundless array

Well I am wanting to change the way my structures are written, currently I use array and I need to limit its use, but I wanted a way to create a dynamic array that is the size of the reading done, without always having to edit the array value.
Current Code:
struct sr_flag {
int value_flag;
};
struct er_time {
int value_time;
};
struct se_option {
struct sr_flag flag[50];
struct er_time time[50];
};
struct read_funcs
struct se_option *option;
void (*option_func) (void);
...
}
struct read_funcs func_;
struct read_funcs *func;
int sr_flags(int i, int fg, int val) {
if(i < 0)
return 0;
return func->option[i].flag[fg].value_flag = val;
}
void option_func(void) {
struct se_option fnc;
fnc.option = malloc(500 * sizeof(*(fnc.option)));
}
void read_fnc() {
func = &func_;
func->option = NULL;
func->option_func = option_func;
}
I look for a way to remove the array amount [50] instead each time the sr_flags function is executed the limit is raised
Example: sr_flags function executed 1x array would be [1] if executed 2x would be [2]
I also think about doing the same with the option_func function
I tried using the following more unsuccessfully
struct se_option {
struct sr_flag *flag;
struct er_time time[50];
};
int sr_flags(int i, int fg, int val) {
if(i < 0)
return 0;
func->option[i].flag = malloc(1 * sizeof(*(func->option[i].flag)));
return func->option[i].flag[fg].value_flag = val;
}
int main () {
for(int i < 0; i < 10; i++)
sr_flags(i, 1, 30);
return 0;
}
I'm not 100% certain on what it is you want but I think you just want to call realloc and increase the size by the amount you provide. And that's very easy to do, as for the values you want with the arrays I'm not sure so I just used a placeholder value.
#include <stdio.h>
#include <stdlib.h>
struct sr_flag {
int value_flag;
};
struct er_time {
int value_time;
};
struct se_option {
struct sr_flag* flag;
struct er_time* time;
};
void allocateflags(struct se_option* options, int size, int val){
options->flag = realloc(options->flag, size*sizeof(struct sr_flag));
struct sr_flag* flag = options->flag+size-1;
flag->value_flag = val;
}
void allocatetime(struct se_option* options,int size, int val){
options->time = realloc(options->time, size*sizeof(struct er_time));
struct er_time* time = options->time+size-1;
time->value_time = val;
}
void displayflagvalues(struct se_option* options,int size){
for(int index = 0; index < size ; ++index){
printf("flag: %i\n",options->flag[index].value_flag);
}
}
void displaytimevalues(struct se_option* options, int size){
for(int index = 0; index < size ; ++index){
printf("time: %i\n",options->time[index].value_time);
}
}
int main(){
struct se_option options = {0};
for(int index = 0; index < 10; ++index){
allocateflags(&options, index,index);
allocatetime(&options, index,index);
}
displayflagvalues(&options, 10);
displaytimevalues(&options,10);
return 0;
}
The code creates an se_option structure wheren sr_flag and er_time pointers are null. Then there's two functions one allocateflags and the other allocatetime, both of which call realloc with the size you provide. When you call realloc, all previous memory is copied over to the new array. Also free is called automatically by realloc.
This step
struct sr_flag* flag = options->flag+size-1;
flag->value_flag = val;
struct er_time* time = options->time+size-1;
time->value_time = val;
Is slightly redundant but it was just to show the newest array can hold the value. If you understand pointer arithmetic, all its doing is incrementing the pointer to the last position then subtracting 1 struct size and setting that value. Basically setting the value of the final array in the pointer.

Getting a seg-fault when setting value of pointer

There really isnt much I can say here.
Here is my lexer file:
#include <ctype.h>
#include <stdio.h>
#include "vector.h"
enum TokenType
{
tok_let = -1,
tok_iden = -2,
tok_int = -3,
tok_end = -4
};
typedef struct
{
int type;
char* str_d;
int int_d;
} Token;
char* seed;
int i=0;
char next_char()
{
i++;
return seed[i-1];
}
vector* get_tokens(char* in)
{
vector *toks;
vector_new(toks);
seed = in;
char tap;
if(isalpha(tap = next_char()))
{
char* iden_str="";
iden_str += tap;
char nc;
while(isalnum((nc = next_char())))
iden_str += nc;
if(iden_str == "let")
{
Token* tp;
tp->type = tok_let;
vector_push(toks, (void*)tp);
goto out;
}
Token* tp;
tp->type = tok_iden;
tp->str_d = iden_str;
vector_push(toks, (void*)tp);
}
out:
return toks;
}
int main()
{
vector* toks;
toks = get_tokens("let");
Token* ftok = (Token*)vector_get(toks, 0);
switch(ftok->type)
{
case tok_let:
printf("Its a let\n");
break;
default:
printf("Ummm lol nup\n");
break;
}
}
And here is my vector file:
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct d_vector
{
void **items;
int capacity;
int total;
} vector;
void vector_new(vector *v)
{
v->capacity = 4;
v->total = 0;
v->items = malloc(sizeof(void*)*v->capacity);
}
int vector_total(vector *v)
{
return v->total;
}
static void vector_resize(vector *v, int capacity)
{
void** items = realloc(v->items, sizeof(void*) * capacity);
if(items)
{
v->items = items;
v->capacity = capacity;
}
}
void vector_push(vector *v, void* item)
{
if(v->capacity == v->total)
vector_resize(v, v->capacity * 2);
v->items[v->total++] = item;
}
void vector_set(vector *v, int index, void* item)
{
if(index >= 0 && index < v->total)
v->items[index] = item;
}
void* vector_get(vector *v, int index)
{
if(index >= 0 && index < v->total)
return v->items[index];
return NULL;
}
void vector_remove(vector *v, int index)
{
if(index < 0 || index >= v->total)
return;
v->items[index] = NULL;
for (int i = 0; i < v->total - 1; i++) {
v->items[i] = v->items[i + 1];
v->items[i + 1] = NULL;
}
v->total--;
if (v->total > 0 && v->total == v->capacity / 4)
vector_resize(v, v->capacity / 2);
}
void vector_free(vector *v)
{
free(v->items);
}
When I run the code above, I get a Seg-Fault.
How can this be happening? Here is the output of gdb:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400656 in vector_new (v=0x1) at vector.h:14
14 v->capacity = 4;
As you can see, its segfaulting when i set the vector capacity!
But why?
It segfaults because you dereference a garbage pointer:
vector* get_tokens(char* in)
{
vector *toks;
vector_new(toks);
The variable toks is not assigned to anything meaningful, just whatever garbage value happens to be floating about. This gets passed into vector_new() which immediately dereferences it:
void vector_new(vector *v)
{
v->capacity = 4;
Then BAM! it blows up because v points nowhere appropriate.
Try mallocing a vector before making your call to vector_new() or put the malloc in vector_new() and have it return the pointer to the new vector instead. It's also a good idea to check the return value from malloc().
You might try something like:
vector *vector_new(void)
{
vector *v;
if ( (v = malloc(sizeof(*v))) == NULL ) {
/* Replace with something appropriate */
exit(EXIT_FAILURE);
}
v->capacity = 4;
v->total = 0;
if ( (v->items = malloc(sizeof(*v->items)*v->capacity)) == NULL ) {
/* Replace with something appropriate */
exit(EXIT_FAILURE);
}
return v;
}
Then change how you call it:
vector* get_tokens(char* in)
{
vector *toks;
toks = vector_new();
And for every malloc(), let there be a free(). Don't forget to clean up this allocation too or you'll leak memory:
void vector_free(vector *v)
{
free(v->items);
free(v);
}
(You defined a vector_free(), but never called it. You might want to consider doing that too.)
invalid pointer dereference happened
vector *toks;
vector_new(toks);
Should be
vector *toks = (vector*)malloc(sizeof(vector));
vector_new(toks);

C Memory Management -> Hash

I don't know if my problem is a memory leak, or i'm not acessing the hashtable in the correct way.
My hash.h
#define HASHSIZE 31
#define EMPTY ""
#define DELETED "-"
typedef char KeyType[9];
typedef void *Info;
typedef struct entry
{
KeyType key;
Info info;
}Entry;
typedef Entry HashTable[HASHSIZE];
My hash.c
int Hash(KeyType k){
return atoi(k)%HASHSIZE;
}
void InitializeTable(HashTable t){
for(int i=0; i < HASHSIZE; i++){
strncpy(t[i].key,EMPTY,9);
}
}
void ClearTable(HashTable t){
InitializeTable(t);
}
void InsertTable_LP(HashTable t, KeyType k, Info i){
int a = 0;
int hash = Hash(k);
while((a<HASHSIZE)
&& strcmp(t[hash].key,EMPTY)!=0
&& strcmp(t[hash].key,DELETED)!=0 ){
hash = (hash + 1) % HASHSIZE;
a++;
}
strncpy(t[hash].key,k,9);
t[hash].info = i;
printf("Value of info is %d\n",(int)t[hash].info);
}
int RetrieveTable_LP(HashTable t, KeyType k){
int a=0;
int hash = Hash(k);
while(a<HASHSIZE
&& strcmp(t[hash].key,k)!=0
&& strcmp(t[hash].key,EMPTY)!=0){
hash=(hash+1) % HASHSIZE;
a++;
}
if(strcmp(t[hash].key,k)==0)
return hash;
return -1;
}
int main(){
HashTable *t = malloc(HASHSIZE*sizeof(Entry));
int valores[] = {1,2,3,4,5,6,7,8,9};
ClearTable(*t);
InsertTable_LP(*t,"1",valores);
InsertTable_LP(*t,"2",valores+1);
InsertTable_LP(*t,"3",valores+2);
InsertTable_LP(*t,"4",valores+3);
InsertTable_LP(*t,"5",valores+4);
int pos = RetrieveTable_LP(*t,"2");
if(pos==-1){
printf("Error\n");
}
else
printf("Position %d\n",pos);
printf("okay %d\n",(int)t[pos]->info);
printf("asdasdas\n");
return 1;
}
My output is
Value of info is 1537727040
Value of info is 1537727044
Value of info is 1537727048
Value of info is 1537727052
Value of info is 1537727056
Position 2
okay 0
If anyone could explain me, thanks in advance.
valores is an array. You are inserting Info which has been typedefed to void *. You need to fix those things.
Your malloc is not necessary the reason why that wasn't obvious is because it was hidden by the way you typedefd the HashTable, don't ever do that, the following code works as you expected yours to do
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASHSIZE 31
#define EMPTY ""
#define DELETED "-"
typedef char KeyType[9];
typedef void *Info;
typedef struct entry
{
KeyType key;
Info info;
}Entry;
typedef Entry HashTable[HASHSIZE];
int Hash(KeyType k){
return atoi(k)%HASHSIZE;
}
void InitializeTable(HashTable t) {
int i;
for(i=0; i < HASHSIZE; i++) {
strncpy(t[i].key, EMPTY, 9);
}
}
void ClearTable(HashTable t) {
InitializeTable(t);
}
void InsertTable_LP(HashTable t, KeyType k, Info i){
int a = 0;
int hash = Hash(k);
while((a<HASHSIZE) && strcmp(t[hash].key, EMPTY) !=0 && strcmp(t[hash].key, DELETED) !=0 ) {
hash = (hash + 1) % HASHSIZE;
a++;
}
strncpy(t[hash].key, k, 9);
t[hash].info = i;
printf("Value of info is %p\n", t[hash].info);
}
int RetrieveTable_LP(HashTable t, KeyType k){
int a=0;
int hash = Hash(k);
while(a<HASHSIZE
&& strcmp(t[hash].key,k)!=0
&& strcmp(t[hash].key,EMPTY)!=0){
hash=(hash+1) % HASHSIZE;
a++;
}
printf("%s, %s\n", t[hash].key, k);
if(strcmp(t[hash].key, k)==0)
return hash;
return -1;
}
int main(){
/*
* You don't need to malloc, since HashTable is an array,
* and it does not need to be a pointer, since it decays
* to one when passed as such.
*/
HashTable t;// = malloc(HASHSIZE * sizeof(Entry));
int valores[] = {1,2,3,4,5,6,7,8,9};
ClearTable(t);
InsertTable_LP(t,"1",valores);
InsertTable_LP(t,"2",valores+1);
InsertTable_LP(t,"3",valores+2);
InsertTable_LP(t,"4",valores+3);
InsertTable_LP(t,"5",valores+4);
int pos = RetrieveTable_LP(t, "2");
if(pos==-1) {
printf("Error\n");
}
else
{
printf("Position %d\n",pos);
printf("okay %p\n", t[pos].info);
}
printf("asdasdas\n");
return 1;
}
your typedef of the HashTable makes it hard to know what to do with a HashTable type variable, that is not a very good use of typedef.
Also the second printf will be executed regardless of the condition
else
printf("Position %d\n",pos);
printf("okay %d\n",(int)t[pos]->info);
you need to add {
else
{
printf("Position %d\n",pos);
printf("okay %d\n",(int)t[pos]->info);
}

Function to read in a word into a struct array

I am having an error with the code we are using, was wondering if someone could help debug. Seems like we are getting a malloc error. Thanks.
void readWords(char norm_word[MAXSIZE], Word ** array) {
int i = 0;
bool found = false;
int result = 0;
Word * current_pointer = malloc (sizeof(Word*));//creates a temporary variable for each pointer in the array
for (i=0; i<word_counter; i++) {
current_pointer = *(array+i); //accesses the current pointer
result = strcmp(norm_word, (current_pointer -> word)); //compares the string to each stored string
if (result == 0) {
found = true;
(current_pointer->freq)++;
break;
}
}
if(!found) {
if(pointer_counter == word_counter) {
array = realloc(array, sizeof(array)*2);
pointer_counter*=2;
}
Word * new_pointer = (Word*) malloc (sizeof(Word*));
strcpy(new_pointer -> word, norm_word);
*(array + (pointer_counter - 1)) = new_pointer;
word_counter++;
}
;
}
All pointers have the same size on your system. So a sizeof always returns the same size for any pointer. You want to allocate for the structure, so you need to use sizeof on the name without the star. malloc will return the pointer to that block of memory afterwards.
Here is a short implementation:
#include <iostream>
#include <string>
typedef struct
{
int num;
int numnum;
}numbers;
int main(int argc, char ** argv)
{
numbers* n = (numbers*)malloc(sizeof(numbers));
n->num = 1;
n->numnum = 2;
free(n);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MAXSIZE 64
typedef struct word {
char word[MAXSIZE];
int freq;
} Word;
int word_counter = 0;
size_t pointer_counter = 16;//Number of pointers that ensure
void readWords(char norm_word[MAXSIZE], Word ** array) {
int i = 0;
bool found = false;
Word *current_pointer = *array;
for (i=0; i<word_counter; i++) {
if(strcmp(norm_word, current_pointer->word) == 0){
found = true;
current_pointer->freq++;
break;
}
++current_pointer;
}
if(!found) {
if(pointer_counter == word_counter) {
pointer_counter *= 2;
*array = realloc(*array, sizeof(Word)*pointer_counter);
}
Word *new_pointer = *array + word_counter;
new_pointer->freq = 1;
strcpy(new_pointer->word, norm_word);
++word_counter;
}
}
int main(void){
Word *vocabulary = calloc(pointer_counter, sizeof(Word));
char norm_word[MAXSIZE];
while(1==scanf("%s", norm_word)){
readWords(norm_word, &vocabulary);
}
{
int i;
for(i = 0; i < word_counter; ++i){
printf("%s(%d)\n", vocabulary[i].word, vocabulary[i].freq);
}
}
free(vocabulary);
return 0;
}

Resources