Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:00:26+00:00 2026-06-17T17:00:26+00:00

I’ve been trying to code up a binary tree on my own. It seems

  • 0

I’ve been trying to code up a binary tree on my own. It seems to be working until I want to scanf for a new string to add. The same string works, new string gives me segmentation fault & core dump. I suspect there’s something wrong with mallocing memory of the new element.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//------- wezel -------//
struct node{
char *word;
unsigned int arity;
struct node *left, *right, *parent;
};
struct node *root;
/* Adding new string to tree */
void dopisz(char wordtmp[50], struct node *start){

    if(root==NULL){ // empty tree, add as root
    root=(struct node*)malloc(sizeof *root);
    root->word=(char*)malloc(sizeof wordtmp);
    root->word=strcpy(root->word, wordtmp);
    root->arity=1;
    root->left=NULL;
    root->right=NULL;
    root->parent=NULL;
    }
    else if(strcmp(wordtmp, start->word)==0){
        start->arity=start->arity+1;
    }
    else if(strcmp(wordtmp, start->word)<0){  //if the added element is <
        if(start->left==NULL){ //if there's no left son
            struct node *nowy=(struct node*)malloc(sizeof *root);
            nowy->word=strcpy(nowy->word, wordtmp);
            nowy->arity=1;
            nowy->left=NULL;
            nowy->right=NULL;
            nowy->parent=start;
            start->left=nowy;
        }
        else if(start->left!=NULL){ //if there's left son
            dopisz(wordtmp, start->left);
        }

    }
    else if(strcmp(wordtmp, start->word)>0){  //if the added element is >
        if(start->right==NULL){ //if there's no right son
            struct node *nowy=(struct node*)malloc(sizeof *root);
            nowy->word=strcpy(nowy->word, wordtmp);
            nowy->arity=1;
            nowy->left=NULL;
            nowy->right=NULL;
            nowy->parent=start;
            start->right=nowy;
        }
        else if(start->right!=NULL){ //if there's right son
            dopisz(wordtmp, start->right);
        }

    }


}
//-------looking for minimum -------//
struct node* least(struct node *start){
    if(start->left != NULL){
        return least(start->left);
    }
    else return start;
}

//------- deleting -------//
void usun(){

}
//------- printing -------//
void drukuj(struct node *start){ //printing in order in order
    if(start->left!=NULL){
        drukuj(start->left); 
    }
    printf("%s (%d)\n", start->word, start->arity);
    if(start->right!=NULL){
        drukuj(start->right);
    }
}
//------- main -------//
int main(){
char wordtmp[50];
printf("\t Drzewo Poszukiwan Binarnych \n------------------------\n\n");
int x, y=0;
while(y==0){
    printf("\n MENU: \n 0 -> zakoncz \n 1 -> dopisz\n 2 -> usun\n 3 -> drukuj\n\n"); // 0 - exit, 1 - add, 2 - delete, 3 - print
    scanf("%d", &x);
    switch(x){
    case 0: y++;        break;
    case 1:
    printf("wpisz slowo: ");
    scanf("%s", wordtmp);
    dopisz(wordtmp, root);
    break;
    case 2: usun();     break;
    case 3: drukuj(root);   break;
    }
}
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:00:26+00:00Added an answer on June 17, 2026 at 5:00 pm

    The line

    nowy->word=strcpy(nowy->word, wordtmp);
    

    is wrong. nowy->word doesn’t have any storage points to arbitrary memory. Copying a string to it has undefined results but a seg fault is likely.

    You can fix this by making word a fixed size array in node‘s definition or by allocating memory for it dynamically

    nowy->word=malloc(strlen(wordtmp)+1);
    strcpy(nowy->word, wordtmp);
    

    or

    nowy->word=strdup(wordtmp); // not standard C but available in Posix systems
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I want use html5's new tag to play a wav file (currently only supported
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.