Segmentation Fault in C - c

My code is giving me a segmentation fault and I can't seem to find what I'm doing wrong:
#include <stdio.h>
#include <string.h>
char find(char name[], char allNames[][10], int length)
{
int i=0;
for (i = 0; i < length; i++) {
if (strcmp(allNames[i],name) == 1) {
printf("%i",i);
return *name;
}
}
return -1;
}
main(){
char allNames[][10] = {"cat","dog","frog","log","bog"};
char name[] = "log";
int length=5;
printf("%s",find(name,allNames,length));
}
I'm really keen to understand all the mechanisms happening here and what I'm doing wrong for tomorrows exam. Thanks for your help!
EDIT: Really Appreciate the answers and information guys! I'm really quite new to C and just getting used to what every thing means. The particular exam question I am looking at is :
(a) The following function is intended to find the string name in the array
allNames. If found, it returns the position of name in the array. If not
found, it returns -1. Modify the code so that it works correctly.
int find(char name[], char allNames[][10])
{
for (i = 0; i < 10; i++) {
if (allNames[i] == name) {
return name;
}
}
return -1;
}
And I'm trying to get a program to work within these parameters. Cheers :)

http://coliru.stacked-crooked.com/a/d400c9a56d732446
#include <stdio.h>
#include <string.h>
char* find(char name[], char allNames[][10], int length)
{
int i=0;
for (i = 0; i < length; i++) {
if (!strcmp(allNames[i],name)) {
printf("%i",i);
return name;
}
}
return NULL;
}
int main(){
char allNames[][10] = {"cat","dog","frog","log","bog"};
char name[] = "log";
int length=5;
printf("%s",find(name,allNames,length));
}
Returning a single char will do you no good if you're trying to return a string. I would also suggest that you return a NULL if you cannot find the string.
Also, include the int before main; this is better style.

The direct reason for your Segmentation Fault here is because the code tried to print the char type with %s(which needs an address value).
void main()
{
char c = 'a';
printf("%s", c); // will cause Segmentation fault here
}
Back to your code, that is
char find(char name[], char allNames[][10], int length)//return char
printf("%s",find(name,allNames,length));
The minimal change to make it work as follows,
1) To return char*
char* find(char name[], char allNames[][10], int length)//return char*
{
int i=0;
for (i = 0; i < length; i++) {
if (strcmp(allNames[i],name) == 0) { // here should 0
printf("%i",i);
return name; // change name* to name
}
}
return NULL; // change to NULL
}
//to print
printf("%s",find(name,allNames,length));
2) to return position value
int find(char name[], char allNames[][10])
{
for (i = 0; i < 10; i++) {
if (allNames[i] == name) {
return i; // here, change to return i
}
}
return -1;
}
//then, you can print like this
printf("find at position: %d",find(name,allNames,length));
//or to print string by
int pos = find(name,allNames,length);
if(pos >= 0)
printf("find the string: %s",allNames[pos]);

This code is wrong on several levels.
gcc -Wall -Wextra reveals:
meh.c:15:1: warning: return type defaults to ‘int’ [-Wreturn-type]
main(){
^
meh.c: In function ‘main’:
meh.c:19:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("%s",find(name,allNames,length));
^
meh.c:21:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
What's up with that? Do you compile with warnings enabled?
I am ignoring the lack of indentation.
#include <stdio.h>
#include <string.h>
char find(char name[], char allNames[][10], int length)
What? How about: char *name, **list, int size)
{
int i=0;
Why set it to 0 here?
for (i = 0; i < length; i++) {
if (strcmp(allNames[i],name) == 1) {
printf("%i",i);
return *name;
Have you read strcmp's manpage? It returns ZERO when a string matches, so this code makes no sense.
*name is of type char, but you don't want to return a char. You want to return a pointer, no?
}
}
return -1;
Well, given that you feed that into %s in printf, what do you expect to hapen here? Normally one would return NULL.
}
main(){
This is obsolete syntax, I don't know where you stole it from. Use 'int main(void)'.
char allNames[][10] = {"cat","dog","frog","log","bog"};
Normally people just return such arrays with a NULL pointer, so that these can be iterated over and there is no need to pass anything about the size.
char name[] = "log";
Why not char *name = "log".
int length=5;
Incorrect. It hardcodes the amount of stored strings in allNames table.
printf("%s",find(name,allNames,length));
}

Related

Some issues with "read access violation"

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
typedef struct {
char name[128], code[128];
} info;
info db[3];
info* data=db;
void find (const char *code, int size, ...) {
bool mismatch = true;
va_list arg;
va_start(arg,size);
while (size-- > 0) {
data = va_arg(arg,info*);
printf("%s", data->code);
if (!strcmp(data->code,code))
{
printf("%s [id:%s]\n",data->name,data->code), mismatch = false;
}
}
if (mismatch) printf("No data available!");
return;
}
int main (int argc, char *argv[], char *envp[]) {
const char *spec[] = {
"Physics of Elementary Particles",
"Physics of Hign Energy",
"Low-level Programming"
};
const char *code[] = {
"2396","0812", "0773"
};`enter code here`
for (int count = 0; count < 3; ++count) {
strncpy(db[count].name,spec[count],128);
strncpy(db[count].code,code[count],128);
}
find("0812",3,db[0],db[1],db[2]);
return 0;
}
After running it says "read access violation", although I hope everything is correct, it happens after function "va_arg(arg, info*)". Is it some troubles with stack or decrypting?
The problem is in the function call:
find("0812",3,db[0],db[1],db[2]);
The variadic argument that you're passing in are of type info. However, when you retrieve them with va_arg you're looking for a info *. These don't match up.
You want to pass in the address of each array member.
find("0812",3,&db[0],&db[1],&db[2]);
Also, you need to call va_end at the bottom of find before you return.

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;
}

error: assigning to 'int' from incompatible type 'void'

I am trying to write a function that will take in a string and a character, and will return the index of where the character last occurs. I am having trouble compiling this, as seen by the title of the question, I get an error:
error: assigning to 'int' from incompatible type 'void'
I have tried changing the "void" of the function to "int", and then have tried to get the function to print the result, however this resulted in a different set of errors. I am just confused, because my teacher has always shown us functions that have always had "void" as the return type, and this type of return is the only one our class has looked at so far.
#include <string.h>
#include <stdio.h>
void findLast (char string[], char letter){
int Num[20];
int i;
int count=-1;
for (i=0; i<strlen(string);i++){
if (letter==string[i]){
count=count+1;
Num[count]=i;
}
}
int result=Num[count];
}
int main (void){
char string[20];
char letter;
int result;
printf("Enter your string: ");
scanf("%s", string );
printf("Enter your letter: ");
scanf(" %c", &letter);
result = findLast(string, letter);
printf("%d", result);
return 0;
}
Your code function has type void that means no return value. Just change the type of function as int and return it with the result.
Modified code:
int findLast (char string[], char letter){
int Num[20];
int i;
int count=-1;
for (i=0; i<strlen(string);i++){
if (letter==string[i]){
count=count+1;
Num[count]=i;
}
}
int result=Num[count];
return result;
}
As #HolyBlackCat mentioned, your function needs to return the value, and should be written as follows:
int findLast (char string[], char letter){
return Num[count];
}
The void keyword specifies that nothing will be returned.
The type void indicates that the function does not return a value. Since your function is supposed to return an integer you need to replace void with int and return the result using a return statement. Also the variables Num and count are not needed. In your for loop the function strlen is called multiple times. It is more efficient to use the string terminator as the loop guard. Here is a simplified version:
int findLast(char string[], char letter)
{
int result, i;
result = -1;
i = 0;
while (string[i] != '\0') {
if (string[i] == letter) {
result = i;
}
i++;
}
return result;
}

when compiling I get this error, I do not know what is wrong

I'm passing a matrix to a text file
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int llenarMatriz() {
int matriz[3][3]={1,2,3,4,5,6,7,8,9};
return matriz[3][3];
}
void guardarMatriz(int matriz[3][3]) {
char direccion[]="C:\\Users\\Usuario\\Desktop\\DIBU.txt";
FILE *archivo = fopen(direccion, "w");
if (archivo == NULL) {
exit(EXIT_FAILURE);
}
char linea[20];
sprintf(linea, "%d %d\n", 3, 3);
fputs(linea, archivo);
for (int i = 0; i < 3; i++) {
linea[0] = '\0';
for (int j = 0; j < 3; j++){
char buffer[10];
sprintf(buffer, "%d ", matriz[3][3]);
strcat(linea, buffer);
}
int len = strlen(linea);
linea[len - 1] = '\n';
fputs(linea,archivo);
}
fclose(archivo);
}
int main() {
llenarMatriz();
guardarMatriz(int matriz[3][3]);
system("pause");
return 0;
}
Error message;
In function 'main':
error: expected expression before 'int'
guardarMatriz(int matriz[3][3]);
You have two problems in your code. First you don't initialize a 2-D array correctly. It should look like this:
int matriz[3][3]={{1,2,3},{4,5,6},{7,8,9}};
second, you don't pass a type name to the function call to declare your variable, so it should look like this:
int matriz[3][3];
llenarMatriz();
guardarMatriz(matriz);
guardarMatriz(int matriz[3][3]);
Don't include the type and the dimensions when passing parameters to a function.
Also, when you call a function returning something, you should use the correct type (an int is not able to return a 2D array of int), and you must store the return somewhere.
In this case you can return a compound literal:
void *llenarMatriz(void)
{
return (int [][3]){{1,2,3},{4,5,6},{7,8,9}};
}
and in main:
int main(void) /* void is the correct argument for `main` */
{
int (*matriz)[3] = llenarMatriz(); /* A pointer to an array of int 3 */
guardarMatriz(matriz);
system("pause");
return 0;
}
If you don't want to declare the array inside main you can use the result of the first function as the argument of the second one:
guardarMatriz(llenarMatriz());
or you can pass the compound literal directly:
guardarMatriz((int [][3]){{1,2,3},{4,5,6},{7,8,9}});

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.

Resources