1

The following code give that error. The code is not all mine some parts where given from my professor, my code is under the commend /add your code/. I don't know the line that causes the error and i can't understand anything i find online, I'm only a freshman.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct charact {
char ch;
int occurs;
struct charact *next;
};
typedef struct charact Char;
typedef Char * ListofChar;
typedef Char * CharNode_ptr;
void letters(char name[50], ListofChar * chars_ptr);
void report(ListofChar chars);
Char * createnode(char ch);
int main(void)
{
    char name[50];
    ListofChar chars = NULL;
    scanf("%49s", name);
    letters(name, &chars);
    report(chars);
    return 0;
}
Char * createnode(char ch)
{
    CharNode_ptr newnode_ptr ;
    newnode_ptr = malloc(sizeof (Char));
    newnode_ptr -> ch = ch;
    newnode_ptr -> occurs = 0;
    newnode_ptr -> next = NULL;
    return newnode_ptr;
}
void letters(char name[50], ListofChar * lst_ptr)
{
    /* add your code */
    Char chars[50];
    size_t i,j;\
   /* lst_ptr = malloc(sizeof(name));*/
    memset(chars,0,50*sizeof(ListofChar));
    for(i=0;name[i]!='\0';i++){
        chars[i].ch=name[i];
        for(j=i+1;name[j]!='\0';j++){
            if(name[i]==name[j]){
                chars[i].occurs = j-i;
                break;
            }
        }
    }



    return;
}
void report(ListofChar chars)
{
    /* add your code */
    int i = 0;
    while(!(chars[i].ch=='\0')){
        printf("%c:%d\n",chars[i].ch,chars[i].occurs);
        i++;
    }

    return;
}
user438383
  • 5,716
  • 8
  • 28
  • 43
  • 4
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel May 21 '22 at 10:44
  • Are you sure you're supposed to be using `sizeof(ListofChar)` here: `memset(chars,0,50*sizeof(ListofChar));`? Hint: `chars` is an array of 50 elements of type `Char`, not `ListofChar` – Spikatrix May 21 '22 at 10:55
  • In addition to @Spikatrix comment, `letters()` doesn't modify the `ListofChars` so it remains `NULL` when `report()` is called (and you can't access to `NULL[i]`) – KyreX May 21 '22 at 11:05
  • @Spikatrix i am not 100% that i have to use sizeof(ListofChar), i ha ddone something simillar on an another project that had the same result as this one and it worked so i used it again – mplamplampla May 21 '22 at 11:44
  • In your `letters()` function, you are not making use of `lst_ptr`. – Zakk May 21 '22 at 11:57
  • 1
    Related, I'd start by deleting the type aliases `ListOfChar` and `CharNode_ptr` (why there are *both* in this code is an utter mystery). It's gonna break a lot of code, but everywhere it breaks is currently obfuscated by worthless type aliasing that does *nothing* but make the code *harder*, not easier to read. Then, start talking to [your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) and explain what your plan, line by line, for `letters` is. – WhozCraig May 21 '22 at 13:19
  • @WhozCraig i can't delete them, that part isn't my code it was given by my professor. I can only write code under the /*add your code*/ commend – mplamplampla May 21 '22 at 13:47
  • 1
    Sympathies abound if this is from your professor and you're paying for this. I realize they're probably trying to teach a data structures course, and may be putting the quality of the code they use to do it on a afterthought shelf, but still, that doesn't mean they have to do it with really crappy code. Just look at the code and consider how every use of `ListOfChar`, `CharNode_ptr`, and `Char *` are actually the *same*. Why add noise? C engineers want to see splats (`*`) where pointers are involved, not contrived aliases, and certainly not multiple aliases to the *same thing*. – WhozCraig May 21 '22 at 18:07
  • 1
    I suspect [this](https://godbolt.org/z/sreWz8ME5) is what you're trying to do. All of the array notation you're using working with linked lists should be scrapped. Rarely do the two collide (though the do when using a temporary array for sorting a linked list using one of a number of random-access algorithms). – WhozCraig May 21 '22 at 19:19
  • @WhozCraig it is not exactly what it is supposed to do , but your code really helped me , thank you – mplamplampla May 22 '22 at 09:30

1 Answers1

1

The segmentation fault is happening at line 58 because chars is NULL.

You are passing in letters the variable chars as an argument ,but you don't change it's value. Then you call the function report with chars as an argument but chars is still NULL. So !(chars[i].ch=='\0') is causing the error.

You probably have to remove Char chars[50] and instead use lst_ptr inside the function letters.

user438383
  • 5,716
  • 8
  • 28
  • 43
  • i did removed the Char chars[50] and used the lst_ptr. Also the segmentation fault can't be at line 58 because even when i comment out the report function it still gives me this error – mplamplampla May 21 '22 at 17:42
  • what i have to do is to add code to the existing one in order by giving a word it will print the distance between the same letters. If the letter doesn't show up again it has to print 0. For examble : hello h:0 e:0 l:1 l:0 o:0 – mplamplampla May 21 '22 at 17:51