Conversion of objects attributes to and from pointers as function argument











up vote
0
down vote

favorite












My function in c contains objects node, with two attributes Key* a pointer to a key object and int data. The signature of a method is key_comp(key, key), which requires two keys, however the node object contains a pointer to a key. I have tried using node->key and (Node).key however neither work as arguments for this method, how would you go about converting the key pointer to a key for use in a method?



typedef struct 
{
Node tree_nodes;
unsigned char *is_free;
int size;
} BStree_struct;

typedef BStree_struct BStree;

void insert_helper(int i, BStree bst, Node node){
if (i>= (*bst).size){
printf("Out of the range of the tree. n");
return;
}
if ((*bst).is_free[i]==1){
(*bst).tree_nodes[i] = node;
(*bst).is_free[i] = 0;
}else if (key_comp(bst->tree_nodes[i].key, node.key) >0){
insert_helper(2*i,bst,node);
}else if (key_comp(bst->tree_nodes[i].key, node.key) <0){
insert_helper(2*i+1, bst, node);
}
}

typedef struct {char *name; int id;} Key;
typedef struct {Key *key; int data;} Node;
Key *key_construct(char *in_name, int in_id);
int key_comp(Key key1, Key key2);
void print_key(Key *key);
void print_node(Node node);

typedef struct {Node *tree_nodes; unsigned char *is_free;
int size;} BStree_struct;
typedef BStree_struct* BStree;
BStree bstree_ini(int size);
void bstree_insert(BStree bst, Key *key, int data);
void bstree_traversal(BStree bst);
void bstree_free(BStree bst)









share|improve this question




















  • 1




    Please add your code to the question, it sounds like you can just use *(node->key), but it's hard to say without seeing the code.
    – Kingsley
    Nov 12 at 4:35












  • added the code here is the method, the node contains object of a pointer to a key, however I keep getting errors when compiling
    – Ben French
    Nov 12 at 4:38












  • What are BStree and Node? Pointers? Please update your question with this information.
    – Kingsley
    Nov 12 at 4:39












  • I tried the code you suggested and recieved error, invalid argument type of '->' I added the header files from the two files to the question for supplementary information, I will also add the key_comp method
    – Ben French
    Nov 12 at 4:43












  • This data organisation makes very little sense. Are these types and signatures given to you?
    – n.m.
    Nov 12 at 5:13















up vote
0
down vote

favorite












My function in c contains objects node, with two attributes Key* a pointer to a key object and int data. The signature of a method is key_comp(key, key), which requires two keys, however the node object contains a pointer to a key. I have tried using node->key and (Node).key however neither work as arguments for this method, how would you go about converting the key pointer to a key for use in a method?



typedef struct 
{
Node tree_nodes;
unsigned char *is_free;
int size;
} BStree_struct;

typedef BStree_struct BStree;

void insert_helper(int i, BStree bst, Node node){
if (i>= (*bst).size){
printf("Out of the range of the tree. n");
return;
}
if ((*bst).is_free[i]==1){
(*bst).tree_nodes[i] = node;
(*bst).is_free[i] = 0;
}else if (key_comp(bst->tree_nodes[i].key, node.key) >0){
insert_helper(2*i,bst,node);
}else if (key_comp(bst->tree_nodes[i].key, node.key) <0){
insert_helper(2*i+1, bst, node);
}
}

typedef struct {char *name; int id;} Key;
typedef struct {Key *key; int data;} Node;
Key *key_construct(char *in_name, int in_id);
int key_comp(Key key1, Key key2);
void print_key(Key *key);
void print_node(Node node);

typedef struct {Node *tree_nodes; unsigned char *is_free;
int size;} BStree_struct;
typedef BStree_struct* BStree;
BStree bstree_ini(int size);
void bstree_insert(BStree bst, Key *key, int data);
void bstree_traversal(BStree bst);
void bstree_free(BStree bst)









share|improve this question




















  • 1




    Please add your code to the question, it sounds like you can just use *(node->key), but it's hard to say without seeing the code.
    – Kingsley
    Nov 12 at 4:35












  • added the code here is the method, the node contains object of a pointer to a key, however I keep getting errors when compiling
    – Ben French
    Nov 12 at 4:38












  • What are BStree and Node? Pointers? Please update your question with this information.
    – Kingsley
    Nov 12 at 4:39












  • I tried the code you suggested and recieved error, invalid argument type of '->' I added the header files from the two files to the question for supplementary information, I will also add the key_comp method
    – Ben French
    Nov 12 at 4:43












  • This data organisation makes very little sense. Are these types and signatures given to you?
    – n.m.
    Nov 12 at 5:13













up vote
0
down vote

favorite









up vote
0
down vote

favorite











My function in c contains objects node, with two attributes Key* a pointer to a key object and int data. The signature of a method is key_comp(key, key), which requires two keys, however the node object contains a pointer to a key. I have tried using node->key and (Node).key however neither work as arguments for this method, how would you go about converting the key pointer to a key for use in a method?



typedef struct 
{
Node tree_nodes;
unsigned char *is_free;
int size;
} BStree_struct;

typedef BStree_struct BStree;

void insert_helper(int i, BStree bst, Node node){
if (i>= (*bst).size){
printf("Out of the range of the tree. n");
return;
}
if ((*bst).is_free[i]==1){
(*bst).tree_nodes[i] = node;
(*bst).is_free[i] = 0;
}else if (key_comp(bst->tree_nodes[i].key, node.key) >0){
insert_helper(2*i,bst,node);
}else if (key_comp(bst->tree_nodes[i].key, node.key) <0){
insert_helper(2*i+1, bst, node);
}
}

typedef struct {char *name; int id;} Key;
typedef struct {Key *key; int data;} Node;
Key *key_construct(char *in_name, int in_id);
int key_comp(Key key1, Key key2);
void print_key(Key *key);
void print_node(Node node);

typedef struct {Node *tree_nodes; unsigned char *is_free;
int size;} BStree_struct;
typedef BStree_struct* BStree;
BStree bstree_ini(int size);
void bstree_insert(BStree bst, Key *key, int data);
void bstree_traversal(BStree bst);
void bstree_free(BStree bst)









share|improve this question















My function in c contains objects node, with two attributes Key* a pointer to a key object and int data. The signature of a method is key_comp(key, key), which requires two keys, however the node object contains a pointer to a key. I have tried using node->key and (Node).key however neither work as arguments for this method, how would you go about converting the key pointer to a key for use in a method?



typedef struct 
{
Node tree_nodes;
unsigned char *is_free;
int size;
} BStree_struct;

typedef BStree_struct BStree;

void insert_helper(int i, BStree bst, Node node){
if (i>= (*bst).size){
printf("Out of the range of the tree. n");
return;
}
if ((*bst).is_free[i]==1){
(*bst).tree_nodes[i] = node;
(*bst).is_free[i] = 0;
}else if (key_comp(bst->tree_nodes[i].key, node.key) >0){
insert_helper(2*i,bst,node);
}else if (key_comp(bst->tree_nodes[i].key, node.key) <0){
insert_helper(2*i+1, bst, node);
}
}

typedef struct {char *name; int id;} Key;
typedef struct {Key *key; int data;} Node;
Key *key_construct(char *in_name, int in_id);
int key_comp(Key key1, Key key2);
void print_key(Key *key);
void print_node(Node node);

typedef struct {Node *tree_nodes; unsigned char *is_free;
int size;} BStree_struct;
typedef BStree_struct* BStree;
BStree bstree_ini(int size);
void bstree_insert(BStree bst, Key *key, int data);
void bstree_traversal(BStree bst);
void bstree_free(BStree bst)






c arrays pointers methods






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 4:45

























asked Nov 12 at 4:33









Ben French

84




84








  • 1




    Please add your code to the question, it sounds like you can just use *(node->key), but it's hard to say without seeing the code.
    – Kingsley
    Nov 12 at 4:35












  • added the code here is the method, the node contains object of a pointer to a key, however I keep getting errors when compiling
    – Ben French
    Nov 12 at 4:38












  • What are BStree and Node? Pointers? Please update your question with this information.
    – Kingsley
    Nov 12 at 4:39












  • I tried the code you suggested and recieved error, invalid argument type of '->' I added the header files from the two files to the question for supplementary information, I will also add the key_comp method
    – Ben French
    Nov 12 at 4:43












  • This data organisation makes very little sense. Are these types and signatures given to you?
    – n.m.
    Nov 12 at 5:13














  • 1




    Please add your code to the question, it sounds like you can just use *(node->key), but it's hard to say without seeing the code.
    – Kingsley
    Nov 12 at 4:35












  • added the code here is the method, the node contains object of a pointer to a key, however I keep getting errors when compiling
    – Ben French
    Nov 12 at 4:38












  • What are BStree and Node? Pointers? Please update your question with this information.
    – Kingsley
    Nov 12 at 4:39












  • I tried the code you suggested and recieved error, invalid argument type of '->' I added the header files from the two files to the question for supplementary information, I will also add the key_comp method
    – Ben French
    Nov 12 at 4:43












  • This data organisation makes very little sense. Are these types and signatures given to you?
    – n.m.
    Nov 12 at 5:13








1




1




Please add your code to the question, it sounds like you can just use *(node->key), but it's hard to say without seeing the code.
– Kingsley
Nov 12 at 4:35






Please add your code to the question, it sounds like you can just use *(node->key), but it's hard to say without seeing the code.
– Kingsley
Nov 12 at 4:35














added the code here is the method, the node contains object of a pointer to a key, however I keep getting errors when compiling
– Ben French
Nov 12 at 4:38






added the code here is the method, the node contains object of a pointer to a key, however I keep getting errors when compiling
– Ben French
Nov 12 at 4:38














What are BStree and Node? Pointers? Please update your question with this information.
– Kingsley
Nov 12 at 4:39






What are BStree and Node? Pointers? Please update your question with this information.
– Kingsley
Nov 12 at 4:39














I tried the code you suggested and recieved error, invalid argument type of '->' I added the header files from the two files to the question for supplementary information, I will also add the key_comp method
– Ben French
Nov 12 at 4:43






I tried the code you suggested and recieved error, invalid argument type of '->' I added the header files from the two files to the question for supplementary information, I will also add the key_comp method
– Ben French
Nov 12 at 4:43














This data organisation makes very little sense. Are these types and signatures given to you?
– n.m.
Nov 12 at 5:13




This data organisation makes very little sense. Are these types and signatures given to you?
– n.m.
Nov 12 at 5:13












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










This is the best I can come up with. It's not clear to me exactly what the code is doing, but having a mix of pointers and object copies is throwing it off a bit.



The key thing to consider is that Node.key is a pointer (to a Key). So to pass these to key_comp(Key a, Key b) you need to de-reference it, thereby converting a Key* to a Key.



void insert_helper(int i, BStree bst, Node node)
{
if (i >= bst.size)
{
printf("Out of the range of the tree. n");
return;
}
if (bst.is_free[i] == 1)
{
bst.tree_nodes[i] = node;
bst.is_free[i] = 0;
}
else if (key_comp(*(bst.tree_nodes[i].key), *(node.key)) > 0)
{
insert_helper(2*i, bst, node);
}
else if (key_comp(*(bst.tree_nodes[i].key), *(node.key)) < 0)
{
insert_helper(2*i+1, bst, node);
}
}


If it were my code, I'd change key_comp() to take a pair of pointers to Key.






share|improve this answer























  • great works perfectly now !
    – Ben French
    Nov 12 at 5:13











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256011%2fconversion-of-objects-attributes-to-and-from-pointers-as-function-argument%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










This is the best I can come up with. It's not clear to me exactly what the code is doing, but having a mix of pointers and object copies is throwing it off a bit.



The key thing to consider is that Node.key is a pointer (to a Key). So to pass these to key_comp(Key a, Key b) you need to de-reference it, thereby converting a Key* to a Key.



void insert_helper(int i, BStree bst, Node node)
{
if (i >= bst.size)
{
printf("Out of the range of the tree. n");
return;
}
if (bst.is_free[i] == 1)
{
bst.tree_nodes[i] = node;
bst.is_free[i] = 0;
}
else if (key_comp(*(bst.tree_nodes[i].key), *(node.key)) > 0)
{
insert_helper(2*i, bst, node);
}
else if (key_comp(*(bst.tree_nodes[i].key), *(node.key)) < 0)
{
insert_helper(2*i+1, bst, node);
}
}


If it were my code, I'd change key_comp() to take a pair of pointers to Key.






share|improve this answer























  • great works perfectly now !
    – Ben French
    Nov 12 at 5:13















up vote
0
down vote



accepted










This is the best I can come up with. It's not clear to me exactly what the code is doing, but having a mix of pointers and object copies is throwing it off a bit.



The key thing to consider is that Node.key is a pointer (to a Key). So to pass these to key_comp(Key a, Key b) you need to de-reference it, thereby converting a Key* to a Key.



void insert_helper(int i, BStree bst, Node node)
{
if (i >= bst.size)
{
printf("Out of the range of the tree. n");
return;
}
if (bst.is_free[i] == 1)
{
bst.tree_nodes[i] = node;
bst.is_free[i] = 0;
}
else if (key_comp(*(bst.tree_nodes[i].key), *(node.key)) > 0)
{
insert_helper(2*i, bst, node);
}
else if (key_comp(*(bst.tree_nodes[i].key), *(node.key)) < 0)
{
insert_helper(2*i+1, bst, node);
}
}


If it were my code, I'd change key_comp() to take a pair of pointers to Key.






share|improve this answer























  • great works perfectly now !
    – Ben French
    Nov 12 at 5:13













up vote
0
down vote



accepted







up vote
0
down vote



accepted






This is the best I can come up with. It's not clear to me exactly what the code is doing, but having a mix of pointers and object copies is throwing it off a bit.



The key thing to consider is that Node.key is a pointer (to a Key). So to pass these to key_comp(Key a, Key b) you need to de-reference it, thereby converting a Key* to a Key.



void insert_helper(int i, BStree bst, Node node)
{
if (i >= bst.size)
{
printf("Out of the range of the tree. n");
return;
}
if (bst.is_free[i] == 1)
{
bst.tree_nodes[i] = node;
bst.is_free[i] = 0;
}
else if (key_comp(*(bst.tree_nodes[i].key), *(node.key)) > 0)
{
insert_helper(2*i, bst, node);
}
else if (key_comp(*(bst.tree_nodes[i].key), *(node.key)) < 0)
{
insert_helper(2*i+1, bst, node);
}
}


If it were my code, I'd change key_comp() to take a pair of pointers to Key.






share|improve this answer














This is the best I can come up with. It's not clear to me exactly what the code is doing, but having a mix of pointers and object copies is throwing it off a bit.



The key thing to consider is that Node.key is a pointer (to a Key). So to pass these to key_comp(Key a, Key b) you need to de-reference it, thereby converting a Key* to a Key.



void insert_helper(int i, BStree bst, Node node)
{
if (i >= bst.size)
{
printf("Out of the range of the tree. n");
return;
}
if (bst.is_free[i] == 1)
{
bst.tree_nodes[i] = node;
bst.is_free[i] = 0;
}
else if (key_comp(*(bst.tree_nodes[i].key), *(node.key)) > 0)
{
insert_helper(2*i, bst, node);
}
else if (key_comp(*(bst.tree_nodes[i].key), *(node.key)) < 0)
{
insert_helper(2*i+1, bst, node);
}
}


If it were my code, I'd change key_comp() to take a pair of pointers to Key.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 5:12

























answered Nov 12 at 5:07









Kingsley

2,07811022




2,07811022












  • great works perfectly now !
    – Ben French
    Nov 12 at 5:13


















  • great works perfectly now !
    – Ben French
    Nov 12 at 5:13
















great works perfectly now !
– Ben French
Nov 12 at 5:13




great works perfectly now !
– Ben French
Nov 12 at 5:13


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256011%2fconversion-of-objects-attributes-to-and-from-pointers-as-function-argument%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine