0

Please consider the following C code in VC++2010 for creating BST in C language. By creating win32 console application in VC++ project.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

typedef struct BSTNode
{
    char *data;
    struct BSTNode *left,*right;

}Node;

Node *createNode(char *str)
{
    int strLength=strlen(str);
    char *data=(char *)malloc(strLength+1);
    strcpy(data,str);
    data[strLength+1]='\0';

    Node *temp=(Node *)malloc(sizeof(Node));
    temp->left=0;
    temp->right=0;
    temp->data=data;

    return temp;
}

int main()
{
    Node *root=createNode("Ravinder");

    printf("%s\n",root->data);
    return 0;
}

It give errors in VC++2010:

warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c   17
warning C4047: 'return' : 'Node *' differs in levels of indirection from 'int'  c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c   25
error C2275: 'Node' : illegal use of this type as an expression c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c   20
error C2223: left of '->right' must point to struct/union   c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c   22
error C2223: left of '->left' must point to struct/union    c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c   21
error C2223: left of '->data' must point to struct/union    c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c   23
error C2065: 'temp' : undeclared identifier c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c   20
error C2065: 'temp' : undeclared identifier c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c   21
error C2065: 'temp' : undeclared identifier c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c   22

But if i replace above function createnode by this it work fine:

Node *createNode(char *str)
{
    Node *temp=(Node *)malloc(sizeof(Node));
    temp->left=0;
    temp->right=0;
    temp->data=str;

    return temp;
}
Paul R
  • 208,748
  • 37
  • 389
  • 560

1 Answers1

4

MSVC 2010 does not support the modern dialects of C such as C99 - it is still only C89, so you need to move all variable declarations to the start of your function, e.g. a cleaned up version of your function might look like this:

Node *createNode(const char *str)
{
    int strLength = strlen(str);
    char *data = malloc(strLength + 1);
    Node *temp = malloc(sizeof(*temp));

    strcpy(data, str); 

    temp->left = NULL;
    temp->right = NULL;
    temp->data = data;

    return temp;
}

Note there was a serious bug here:

data[strLength+1]='\0';

which would have caused an invalid write to unallocated memory.

This should have been:

data[strLength]='\0';

However this operation is redundant anyway, as strcpy will already have written '\0' at the end of the string, so you might as well just delete this line.

Note also that you are casting the result of malloc in many places - this is redundant and potentially dangerous - you should remove these casts.

Community
  • 1
  • 1
Paul R
  • 208,748
  • 37
  • 389
  • 560