Error pops up at structs and double pointers in c - c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHARS 10
char **readText(void) ;
typedef struct text_t
{
char * * mt;
int words;
}
Text_t;
int main(void)
{
int i, words;
Text_t.mt = readText();
for (i=0; i<words; i++)
printf("%s\n", Text_t.mt[i]);
return 0;
}
char **readText(void)
{
*Text_t.mt = NULL;
char *word;
*words = 0;
while (scanf("%s", word=malloc(CHARS*sizeof(char))),
strcmp(word,"END"))
{
(*words) ++;
Text_t.mt = realloc(mytext, (*words)*sizeof(char *));
Text_t.mt[*words-1] = word;
}
free(word);
return Text_t.mt;
}
The goal of this programm is to take some words as input from the user untill the word "END" is entered. Then we print the given words with the same order as they were entered. The problem is when I try to run the programm, this error comes up:
main.c|16|error: expected identifier or '(' before '.' token|

You probably mean
Text_t tt;
tt.mt = NULL;
ie
create an instance ot the Text_t struct
set its 'mt' pointer to NULL
whether thats the correct thing for your main aim I cant say, but at least it will get you past your current compile error

Related

Adding words to a hash table in C

Before proceeding with PSET5 - SPELLER of the CS50 course, I have decided to practice with a made-up program that takes words from a file and sorts them into a Hash Table, but I think I`m doing something wrong with the Hash Function as I keep getting the following error:
array subscript is not an integer
table[hash] = n;
Some of the elements are taken from the task itself to understand how they work. I don`t have any previous knowledge, totally limited to the CS50 course.
Please have a look at my code and maybe give a few pointers to what I am doing wrong.
From what I understand - every new word`s first letter goes through Hash Functions and returns a number for the Bucket in which this word goes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int hash(const char *buffer);
const unsigned int LENGTH = 9;
typedef struct node
{
char word[LENGTH + 1];
struct node* next;
}
node;
node *table[26] = {NULL};
int hash(const char *buffer)
{
return toupper(buffer[0]) - 'A';
}
int main(void)
{
FILE *file = fopen("words", "r");
if (file != NULL)
{
char buffer[LENGTH];
while (fscanf(file, "%s", buffer) != EOF)
{
node *n = malloc(sizeof(node));
if (n == NULL)
{
return 1;
}
strcpy(n->word, buffer);
n->next = NULL;
table[hash] = n;
}
fclose(file);
}
}
You need to call the function hash(..) , it is not a variable.
Your line should be:
table[ hash(n->word) ] = n;

search of string in an array of strings

i wrote some code that is supposed to find the location of a given string in an array of strings.
problem is- it doesn't give the location. it gives something else.
i understand that probably the problem has to do with the differences between the pointers that are involved- a previous version that dealt with finding the position of a letter in a word worked well.
after a lot of attempts to figure out where is the bug, i ask your help.
kindly, explain me what should be done.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int what (char * token);
main()
{
int i=0;
char string[]="jsr";
char *token;
token=&string[0];
i=what(token);
printf(" location of input is %d \n", i);
return 0;
}
int what (char * token)
{
int i=1;
char *typtbl[]={"mov",
"cmp",
"add",
"sub",
"not",
"clr",
"lea",
};
char * ptr;
ptr=(char *)typtbl;
while (!(strcmp(ptr,token)==0))
{
ptr=(char *)(typtbl+i);
i++;
}
return i;
}
As pointed out, you did not design function what properly. What value should it return if your search function go through all the pointers but does not find the desired string? Typically in that case return -1 would be a choice to indicate nothing found. Also in this case, using a for loop would probably be more suitable, you can just return the index immediately instead of going through all pointers.
int what(char *token)
{
char *typtbl[] = {
"mov",
"cmp",
"add",
"sub",
"not",
"clr",
"lea",
};
for( size_t i = 0; i < sizeof(typtbl)/sizeof(char*); ++i )
{
char *ptr = typtbl[i];
if(strcmp(ptr, token) == 0)
{
return i; // found something
}
}
return -1; // found nothing
}
A cleaner working version.
Main issue is in the (char *)(typtbl+i) replaced by typtbl[i] in the following code. typtbl+i is equivalent to &typtbl[i], so if my memory is good, it's a pointer on the pointer of the string and not the pointer of string itself
I added a NULL at the end of the array to be able to stop if the string is not present and return -1 to clearly say it was not found.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int what(char *token);
int main()
{
int i = 0;
char string[] = "jsr";
i = what(string);
printf(" location of input is %d \n", i);
return 0;
}
int what(char *token)
{
char *typtbl[] = {
"mov",
"cmp",
"add",
"jsr",
"not",
"clr",
"lea",
NULL
};
int i = 0;
while(typtbl[i] && !(strcmp(typtbl[i], token) == 0)) {
++i;
}
if(!typtbl[i])
i = -1;
return i;
}
char *token; token=&string[0]; was useless because string == &string[0].
A few things:
Your main function is missing its return type.
The while loop in what doesn't stop when the element isn't found. Therefore you are reading out of bounds.
This should do the work w/o pointer arithmetic.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int what (char * token);
int main(){
int i=0;
char string[]="jsr";
char *token;
token=&string[0];
i=what(token);
printf(" location of input is %d \n", i);
return 0;
}
int what (char * token){
unsigned int i=0;
char *typtbl[]={"mov",
"cmp",
"add",
"sub",
"not",
"clr",
"lea",
};
unsigned int typtbl_x_size = sizeof(typtbl)/sizeof(typtbl[0]);
char * ptr;
ptr=typtbl[i];
while (!(strcmp(ptr,token)==0)){
i += 1;
if (i >= typtbl_x_size){
printf("element not in list\n");
return -1;
}
ptr=typtbl[i];
}
return i;
}

C programming : Creating substring

I'm new in C and I need some explanation on what I am doing wrong.
I'm trying to iterate over a string and find the first '\' then make a substring from that place in the array.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
struct info{
char* name;
char* type;
char* path;
};
struct info user1;
char* a = "/home/users/user1";
for (int i = strlen(a) ; i < 0 ; i--) {
printf("%d",i);
if(strcmp(a[i],'/')==0){
strncpy(a,user1.name,i);
break;
}
}
return 0;
}
There are many errors I will explain them one by one. The code will be something like this.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
struct info{
char* name;
char* type;
char* path;
};
struct info user1;
user1.name = malloc(40);
if( user1.name == NULL){
fprintf(stderr, "%s\n","Error in malloc" );
exit(1);
}
const char* a = "/home/users/user1";
for(int i = strlen(a) -1; i >= 0 ; i--) {
if(a[i]=='/'){
strncpy(user1.name,a+i+1,i);
user1.name[i]='\0';
break;
}
}
printf("%s\n",user1.name );
free(user1.name);
return 0;
}
Things you did wrong
There was no memory allocated to name it was simply an uninitialized pointer. Here we have allocated memory to it.
Second thing, strcmp as the name suggests compares null terminated char array not char-s. It can be done with simple == operator.
The copy part is modified to only copy the user name part nothing else. That's why we have incremented the pointer to point to the correct position.
You forgot to check the return value of malloc and then you should free the allocated memory.
Also you can't modify a string literal because it stays in non-modifiable portion of the memory.
try this,
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
struct info{
char* name;
char* type;
char* path;
};
struct info user1;
user1.name = malloc(10);
char* a = "/home/users/user1";
int len=strlen(a);
for (int i = 0; i < len; i++) {
printf("%d",i);
if(a[i]=='/'){
strncpy(user1.name,a+i+1,i);
user1.name[i]='\0';
break;
}
}
return 0;
}

C - Errors when trying to make a hashtable

I'm working on a hash table that stores strings in linked lists so I can avoid collisions. However, I'm getting two errors that I'm not sure how to fix. The first error I am getting is in the line that says NewT->Table[i] == NULL;. It's saying warning: statement with no effects [-Wunused-value].
The second error I'm getting is in the same function. The error is in the line return NewT and the error is warning: return from incompatible pointer type[enabled by default]. I've been staring at this for awhile and I can't see where there is an unused value and I have no idea what the return error means even after a bit of research. Can someone explain these to me and help me fix them?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define HASH_MULTIPLIER 65599
/*Structures*/
typedef struct List_T
{
char *str;
int count;
struct List_T *next;
} ListT;
typedef struct Hash_T
{
int htsize;
ListT **Table;
} HashT;
/*Prototypes*/
unsigned int hash(const char *str);
HashT **ht_create(void);
int htsize;
int main(int argc, char *argv[])
{
if (argc <= 1)
{
printf("Please declare a table size");
return 1;
}
htsize = atoi(argv[1]);
return 0;
}
unsigned int hash(const char *str)
{
int i;
unsigned int h = 0U;
for (i = 0; str[i] != '\0'; i++)
h = h * HASH_MULTIPLIER + (unsigned char) str[i];
return h % htsize;
}
HashT **ht_create(void)
{
HashT *NewT;
int i;
if (htsize < 1) //invalid size for
{
fprintf(stderr,"Invalid Size for table");
exit(0);
}
if ((NewT = malloc(sizeof(HashT))) == NULL)
{
fprintf(stderr,"Invalid size for table");
exit(0);
}
if ((NewT->Table = malloc(sizeof(ListT *) * htsize)) == NULL)
{
fprintf(stderr,"Invalid size for table");
exit(0);
}
for (i = 0; i<htsize; i++)
{
NewT->Table[i] == NULL;
}
NewT->htsize = htsize;
return NewT;
}
The first error I am getting is in the line that says NewT->Table[i]
== NULL;. It's saying warning: statement with no effects [-Wunused-value].
This error shows up because the code is making a comparison and not an assignment. The value returned by the comparison (is Table[i] null?) is itself not assigned to anything else, which means it's unused.
Keep a single = operator instead of the doubled == to make sure you're actually assigning instead of comparing.
The second error I'm getting is in the same function. The error is in
the line return NewT and the error is warning: return from
incompatible pointer type[enabled by default].
Your function claims to be returning a pointer to a pointer to HashT, or HashT **, but you end up returning a pointer to HashT, or HashT * instead, which is the type of your NewT variable.
Your function's signature should use a single * instead of two.

use strtok to track of the frequency of each word in a text,but code cannot be executed,

I'm trying to use array of structure and strtok to track of the frequency of each word in a text. Every time a word is added for the first time to the array set count to 1. If the same word appears again, increment the count. But code can not be executed, the compile passed with out any error and warning. I don't know how to debug the code. the code is shown below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char word[32];
int count;
}Entry;
int main(void) {
int n=0;
Entry entry[n];
char s[]="C (pronounced like the letter C) is a general programming language";
char *delim=" ; ( ) . , \n";
char *p;
p=strtok(s, delim);
printf("%s\n",p);
strcpy(entry[0].word, p);
entry[0].count=1;
while((p!=NULL)){
/*printf("%s",p);*/
while (n<10){
p=strtok(NULL, delim);
if(p==entry[n].word){
entry[n].count++;
}
else{
strcpy(entry[n+1].word, p);
entry[n+1].count=1;
}
n++;
}
}
return 1;
}
if(p==entry[n].word){
You can't compare strings using ==, change to
if(strcmp(p, entry[n].word) == 0)){
And you are reserving space for 0 elements:
int n=0;
Entry entry[n];

Resources