-2

Why it gives the error?

#include<stdio.h>
#include<stdlib.h>
typedef struct bST
{
    int ID;
    char name[30];
    char surname[30];
    int friendsID[30];
    struct BST *left;
    struct BST *right;
}BST;
int main ()
{


}
BST *newNode(int ID, char name[], char surname[], int friendsID[])
{
    BST *newNodeTemp=(BST*)malloc(sizeof(BST));

    if(newNodeTemp==NULL)
    {
        printf("Not Allowed!!\n");
        exit(0);
    }

    newNodeTemp->ID=ID;
    newNodeTemp->name=name; //error here
    newNodeTemp->surname=surname; // error here
    newNodeTemp->friendsID=friendsID; //error here
}

array type error initialization name surname friends arrays.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
J.Doe123
  • 15
  • 3

3 Answers3

0
newNodeTemp->name=name; //error here
newNodeTemp->surname=surname; // error here
newNodeTemp->friendsID=friendsID; //error here

These gives error because you are trying to assign to arrays i.e using array as lvalue.

For char arrays if they are nul terminated use strcpy(if not then memcpy) and for int array you can use memcpy to copy the content from one to another.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • thank you for answer. I want to send address of name array. what should I do? – J.Doe123 Mar 24 '17 at 18:22
  • @J.Doe123 I don't clearly understand you by copying you can get the content saved but if you want address use pointer for that. – ameyCU Mar 24 '17 at 19:08
  • @J.Doe123 ". I want to send address of name array. what should I do? " -- We don't know because that doesn't mean anything. Try being more precise. – Jim Balter Mar 28 '17 at 07:24
0

For the char array, strcpy() would be fine. For the int array, you can copy item by item (not that elegant, but does the trick) or you can use memcpy() to do the dirty work for you.

Try these modifications inside your code and see if they help:

Copy one by one:

newNodeTemp->ID=ID;
strcpy(newNodeTemp->name,name);
strcpy(newNodeTemp->surname,surname);
for ( i=0; i<30; i++ )
    newNodeTemp->friendsID[i]=friendsID[i];

Using memcpy():

newNodeTemp->ID=ID;
strcpy(newNodeTemp->name,name);
strcpy(newNodeTemp->surname,surname);
memcpy(newNodeTemp->friendsID,friendsID,30);

Prefer using dynamic allocation approaches, as they are more efficient. So, instead of having char name[30], surname[30], friendsID[30], prefer using char *name, char *surname, int *friendsID and use malloc() to ask for as many memory as you need. ;-)

snortt
  • 1
  • 2
-3

I suggest using a different language than C. C is ancient and primitive and does not work the way you're conceiving of it. Notably, your function signature is unworkable because arrays are not first class entities in C, and so that signature is exactly equivalent to

BST *newNode(int ID, char* name, char* surname, int* friendsID) ...

As you can see, these are simply pointers to the data; they have no length associated with them. Whenever passing arrays to functions in C, you must always pass the length of the array as well, unless there's some other way to determine the length -- e.g., being NUL-terminated, as the convention for C "strings" (in scare quotes because there is no string datatype in C).

Note that the arrays in BST have fixed size lengths, so you are begging for array overruns. It would be better to declare these as pointers (note other improvements in the declaration syntax):

typedef struct BST BST;
struct BST
{
    int ID;
    char* name;
    char* surname;
    int* friendsID;
    BST* left;
    BST* right;
};

Then you could have (note various improvements and corrections)

BST *newBST(int ID, char* name, char* surname, int* friendsID)
{
    BST* bst = malloc(sizeof *bst);

    if (!bst)
    {
        fprintf(stderr, "Out of memory.\n");
        exit(1);
    }

    bst->ID = ID;
    bst->name = name;
    bst->surname = surname;
    bst->friendsID = friendsID;
    bst->left = bst->right = NULL;
    return bst;
}

provided that the name, surname, and friendsID arguments were all malloced and that friendsID is terminated with a sentinel value or that you have some other way to know its length. Otherwise, you need to pass the number of friends as an argument to newBST and store it in your BST node.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
  • Thank you very much, jim. Unfortunately, I can not translate my homework because its Turkish. Subject is Linked List based Binary Search Tree. I have to write some function such as insertNewUser, deleteUser, displaySize, sortGreater, displayUser'sFriends, sortUser'sFriends....etc – J.Doe123 Mar 28 '17 at 16:28
  • I have a new question. – J.Doe123 Mar 30 '17 at 20:06