Editing Array of Struct in another Function












0














As I may or may not have stated in a previous question, I am a newbie to C Programming and only have experience previously with Java, so I am fairly incompetent at using Structs and Pointers and may not have grasped these concepts well on the first time.



So I was playing with C and when I tried out the following program below:



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define buf_sz 32

struct friends {
char name[buf_sz];
int number;
};
typedef struct friends friend;

void addfriend (friend * friendArr, int index);
void removefriend (friend * friendArr);

void main () {

int index;
int troll;

friend friendArr[50];

printf("1. Add Friend Name, 2. Remove Friend. Other buttons. Do nothingn");
scanf(" %d", &troll);

while ((troll == 1) && (troll == 2)) {
switch (troll) {
case 1: addfriend(friendArr, index);
index++;
break;
case 2: removefriend(friendArr);
break;
default: break;
}
}

for (int i = 0; i < 50; i++) {
if (friendArr[i].name != NULL) {
printf("%st%dn", friendArr[i].name, friendArr[i].number);
}
}

}

void addfriend (friend * friendArr, int index) {

char buf[buf_sz];
int number;

printf("Add a friend's namen");
fgets(buf, buf_sz-1, stdin);
sscanf(buf, " %s", friendArr[index].name);

printf("Add his numbern");

scanf(" %d", &number);
friendArr[index].number = number;

}

void removefriend (friend * friendArr) {

char buf[buf_sz];
char name[buf_sz];
int check;

printf("Add a friend's name you wanna removen");

fgets(buf, buf_sz-1, stdin);
sscanf(buf, " %s", name);

for (int i = 0; i < 50; i++) {
if (friendArr[i].name == name) {
strcpy(friendArr[i].name, "");
}
}

}


My Output was given as:



1. Add Friend Name, 2. Remove Friend. Other buttons. Do nothing
1
@R 2009984
0
624
4
v 144
0
l� 685382481
32767
��G� 364104144
0
0�G� 368262747
32517
@�G� 440903072
� 32517
368167105
32517
�G� 370332944
32517
_�g 370428400
32517
368167105
32517
364032000
0
0
32517
�I 368217097
0
��G� 368166640
32517
��� 370428400
0
' 0
0
pQ 368182487
32517
370431760
0
��� 0
0
8W 0
32767
W 440904224
� 32517
��� 370432664
0
0
32517
��G� 368232864
32767


Which is basically a bunch of strange numbers, which I do not get the meaning of.



The desired output would be a message to print the question add a user's name and number if 1 was pressed, and delete a user if 2 was pressed, then print all the users.



I believe the problem here is my passing of array to struct, but I am not sure.



I have searched Stack Overflow everywhere for possible places I have went wrong, but I could not find anything that helped.



Can you friendly people point out where I went wrong?










share|improve this question




















  • 1




    You never initialize index, so index++; causes undefined behavior.
    – Barmar
    Nov 20 at 22:18










  • You cannot compare strings with == in C. The best thing you can do at this point is to 1) turn on all warnings in your compiler and set them to be treated as errors, and 2) learn how to use a debugger.
    – Groo
    Nov 20 at 22:19












  • (troll == 1) && (troll == 2) can never be true. How can a variable be equal to two different numbers at the same time? I think you meant ||.
    – Barmar
    Nov 20 at 22:19










  • I believe that I summarised index as an int. Are you talking about the index in the main function?
    – Ervin Tan
    Nov 20 at 22:20










  • for (int i = 0; i < 50; i++) is printing more friends than have actually been added.
    – Barmar
    Nov 20 at 22:20
















0














As I may or may not have stated in a previous question, I am a newbie to C Programming and only have experience previously with Java, so I am fairly incompetent at using Structs and Pointers and may not have grasped these concepts well on the first time.



So I was playing with C and when I tried out the following program below:



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define buf_sz 32

struct friends {
char name[buf_sz];
int number;
};
typedef struct friends friend;

void addfriend (friend * friendArr, int index);
void removefriend (friend * friendArr);

void main () {

int index;
int troll;

friend friendArr[50];

printf("1. Add Friend Name, 2. Remove Friend. Other buttons. Do nothingn");
scanf(" %d", &troll);

while ((troll == 1) && (troll == 2)) {
switch (troll) {
case 1: addfriend(friendArr, index);
index++;
break;
case 2: removefriend(friendArr);
break;
default: break;
}
}

for (int i = 0; i < 50; i++) {
if (friendArr[i].name != NULL) {
printf("%st%dn", friendArr[i].name, friendArr[i].number);
}
}

}

void addfriend (friend * friendArr, int index) {

char buf[buf_sz];
int number;

printf("Add a friend's namen");
fgets(buf, buf_sz-1, stdin);
sscanf(buf, " %s", friendArr[index].name);

printf("Add his numbern");

scanf(" %d", &number);
friendArr[index].number = number;

}

void removefriend (friend * friendArr) {

char buf[buf_sz];
char name[buf_sz];
int check;

printf("Add a friend's name you wanna removen");

fgets(buf, buf_sz-1, stdin);
sscanf(buf, " %s", name);

for (int i = 0; i < 50; i++) {
if (friendArr[i].name == name) {
strcpy(friendArr[i].name, "");
}
}

}


My Output was given as:



1. Add Friend Name, 2. Remove Friend. Other buttons. Do nothing
1
@R 2009984
0
624
4
v 144
0
l� 685382481
32767
��G� 364104144
0
0�G� 368262747
32517
@�G� 440903072
� 32517
368167105
32517
�G� 370332944
32517
_�g 370428400
32517
368167105
32517
364032000
0
0
32517
�I 368217097
0
��G� 368166640
32517
��� 370428400
0
' 0
0
pQ 368182487
32517
370431760
0
��� 0
0
8W 0
32767
W 440904224
� 32517
��� 370432664
0
0
32517
��G� 368232864
32767


Which is basically a bunch of strange numbers, which I do not get the meaning of.



The desired output would be a message to print the question add a user's name and number if 1 was pressed, and delete a user if 2 was pressed, then print all the users.



I believe the problem here is my passing of array to struct, but I am not sure.



I have searched Stack Overflow everywhere for possible places I have went wrong, but I could not find anything that helped.



Can you friendly people point out where I went wrong?










share|improve this question




















  • 1




    You never initialize index, so index++; causes undefined behavior.
    – Barmar
    Nov 20 at 22:18










  • You cannot compare strings with == in C. The best thing you can do at this point is to 1) turn on all warnings in your compiler and set them to be treated as errors, and 2) learn how to use a debugger.
    – Groo
    Nov 20 at 22:19












  • (troll == 1) && (troll == 2) can never be true. How can a variable be equal to two different numbers at the same time? I think you meant ||.
    – Barmar
    Nov 20 at 22:19










  • I believe that I summarised index as an int. Are you talking about the index in the main function?
    – Ervin Tan
    Nov 20 at 22:20










  • for (int i = 0; i < 50; i++) is printing more friends than have actually been added.
    – Barmar
    Nov 20 at 22:20














0












0








0







As I may or may not have stated in a previous question, I am a newbie to C Programming and only have experience previously with Java, so I am fairly incompetent at using Structs and Pointers and may not have grasped these concepts well on the first time.



So I was playing with C and when I tried out the following program below:



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define buf_sz 32

struct friends {
char name[buf_sz];
int number;
};
typedef struct friends friend;

void addfriend (friend * friendArr, int index);
void removefriend (friend * friendArr);

void main () {

int index;
int troll;

friend friendArr[50];

printf("1. Add Friend Name, 2. Remove Friend. Other buttons. Do nothingn");
scanf(" %d", &troll);

while ((troll == 1) && (troll == 2)) {
switch (troll) {
case 1: addfriend(friendArr, index);
index++;
break;
case 2: removefriend(friendArr);
break;
default: break;
}
}

for (int i = 0; i < 50; i++) {
if (friendArr[i].name != NULL) {
printf("%st%dn", friendArr[i].name, friendArr[i].number);
}
}

}

void addfriend (friend * friendArr, int index) {

char buf[buf_sz];
int number;

printf("Add a friend's namen");
fgets(buf, buf_sz-1, stdin);
sscanf(buf, " %s", friendArr[index].name);

printf("Add his numbern");

scanf(" %d", &number);
friendArr[index].number = number;

}

void removefriend (friend * friendArr) {

char buf[buf_sz];
char name[buf_sz];
int check;

printf("Add a friend's name you wanna removen");

fgets(buf, buf_sz-1, stdin);
sscanf(buf, " %s", name);

for (int i = 0; i < 50; i++) {
if (friendArr[i].name == name) {
strcpy(friendArr[i].name, "");
}
}

}


My Output was given as:



1. Add Friend Name, 2. Remove Friend. Other buttons. Do nothing
1
@R 2009984
0
624
4
v 144
0
l� 685382481
32767
��G� 364104144
0
0�G� 368262747
32517
@�G� 440903072
� 32517
368167105
32517
�G� 370332944
32517
_�g 370428400
32517
368167105
32517
364032000
0
0
32517
�I 368217097
0
��G� 368166640
32517
��� 370428400
0
' 0
0
pQ 368182487
32517
370431760
0
��� 0
0
8W 0
32767
W 440904224
� 32517
��� 370432664
0
0
32517
��G� 368232864
32767


Which is basically a bunch of strange numbers, which I do not get the meaning of.



The desired output would be a message to print the question add a user's name and number if 1 was pressed, and delete a user if 2 was pressed, then print all the users.



I believe the problem here is my passing of array to struct, but I am not sure.



I have searched Stack Overflow everywhere for possible places I have went wrong, but I could not find anything that helped.



Can you friendly people point out where I went wrong?










share|improve this question















As I may or may not have stated in a previous question, I am a newbie to C Programming and only have experience previously with Java, so I am fairly incompetent at using Structs and Pointers and may not have grasped these concepts well on the first time.



So I was playing with C and when I tried out the following program below:



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define buf_sz 32

struct friends {
char name[buf_sz];
int number;
};
typedef struct friends friend;

void addfriend (friend * friendArr, int index);
void removefriend (friend * friendArr);

void main () {

int index;
int troll;

friend friendArr[50];

printf("1. Add Friend Name, 2. Remove Friend. Other buttons. Do nothingn");
scanf(" %d", &troll);

while ((troll == 1) && (troll == 2)) {
switch (troll) {
case 1: addfriend(friendArr, index);
index++;
break;
case 2: removefriend(friendArr);
break;
default: break;
}
}

for (int i = 0; i < 50; i++) {
if (friendArr[i].name != NULL) {
printf("%st%dn", friendArr[i].name, friendArr[i].number);
}
}

}

void addfriend (friend * friendArr, int index) {

char buf[buf_sz];
int number;

printf("Add a friend's namen");
fgets(buf, buf_sz-1, stdin);
sscanf(buf, " %s", friendArr[index].name);

printf("Add his numbern");

scanf(" %d", &number);
friendArr[index].number = number;

}

void removefriend (friend * friendArr) {

char buf[buf_sz];
char name[buf_sz];
int check;

printf("Add a friend's name you wanna removen");

fgets(buf, buf_sz-1, stdin);
sscanf(buf, " %s", name);

for (int i = 0; i < 50; i++) {
if (friendArr[i].name == name) {
strcpy(friendArr[i].name, "");
}
}

}


My Output was given as:



1. Add Friend Name, 2. Remove Friend. Other buttons. Do nothing
1
@R 2009984
0
624
4
v 144
0
l� 685382481
32767
��G� 364104144
0
0�G� 368262747
32517
@�G� 440903072
� 32517
368167105
32517
�G� 370332944
32517
_�g 370428400
32517
368167105
32517
364032000
0
0
32517
�I 368217097
0
��G� 368166640
32517
��� 370428400
0
' 0
0
pQ 368182487
32517
370431760
0
��� 0
0
8W 0
32767
W 440904224
� 32517
��� 370432664
0
0
32517
��G� 368232864
32767


Which is basically a bunch of strange numbers, which I do not get the meaning of.



The desired output would be a message to print the question add a user's name and number if 1 was pressed, and delete a user if 2 was pressed, then print all the users.



I believe the problem here is my passing of array to struct, but I am not sure.



I have searched Stack Overflow everywhere for possible places I have went wrong, but I could not find anything that helped.



Can you friendly people point out where I went wrong?







c arrays struct






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:17









Barmar

418k34243344




418k34243344










asked Nov 20 at 22:15









Ervin Tan

11




11








  • 1




    You never initialize index, so index++; causes undefined behavior.
    – Barmar
    Nov 20 at 22:18










  • You cannot compare strings with == in C. The best thing you can do at this point is to 1) turn on all warnings in your compiler and set them to be treated as errors, and 2) learn how to use a debugger.
    – Groo
    Nov 20 at 22:19












  • (troll == 1) && (troll == 2) can never be true. How can a variable be equal to two different numbers at the same time? I think you meant ||.
    – Barmar
    Nov 20 at 22:19










  • I believe that I summarised index as an int. Are you talking about the index in the main function?
    – Ervin Tan
    Nov 20 at 22:20










  • for (int i = 0; i < 50; i++) is printing more friends than have actually been added.
    – Barmar
    Nov 20 at 22:20














  • 1




    You never initialize index, so index++; causes undefined behavior.
    – Barmar
    Nov 20 at 22:18










  • You cannot compare strings with == in C. The best thing you can do at this point is to 1) turn on all warnings in your compiler and set them to be treated as errors, and 2) learn how to use a debugger.
    – Groo
    Nov 20 at 22:19












  • (troll == 1) && (troll == 2) can never be true. How can a variable be equal to two different numbers at the same time? I think you meant ||.
    – Barmar
    Nov 20 at 22:19










  • I believe that I summarised index as an int. Are you talking about the index in the main function?
    – Ervin Tan
    Nov 20 at 22:20










  • for (int i = 0; i < 50; i++) is printing more friends than have actually been added.
    – Barmar
    Nov 20 at 22:20








1




1




You never initialize index, so index++; causes undefined behavior.
– Barmar
Nov 20 at 22:18




You never initialize index, so index++; causes undefined behavior.
– Barmar
Nov 20 at 22:18












You cannot compare strings with == in C. The best thing you can do at this point is to 1) turn on all warnings in your compiler and set them to be treated as errors, and 2) learn how to use a debugger.
– Groo
Nov 20 at 22:19






You cannot compare strings with == in C. The best thing you can do at this point is to 1) turn on all warnings in your compiler and set them to be treated as errors, and 2) learn how to use a debugger.
– Groo
Nov 20 at 22:19














(troll == 1) && (troll == 2) can never be true. How can a variable be equal to two different numbers at the same time? I think you meant ||.
– Barmar
Nov 20 at 22:19




(troll == 1) && (troll == 2) can never be true. How can a variable be equal to two different numbers at the same time? I think you meant ||.
– Barmar
Nov 20 at 22:19












I believe that I summarised index as an int. Are you talking about the index in the main function?
– Ervin Tan
Nov 20 at 22:20




I believe that I summarised index as an int. Are you talking about the index in the main function?
– Ervin Tan
Nov 20 at 22:20












for (int i = 0; i < 50; i++) is printing more friends than have actually been added.
– Barmar
Nov 20 at 22:20




for (int i = 0; i < 50; i++) is printing more friends than have actually been added.
– Barmar
Nov 20 at 22:20












1 Answer
1






active

oldest

votes


















0














You need to initialise your variables:



void main () {

int index = 0; // <<-- THIS
int troll;

// Make some friends
friend friendArr[50];

for (int i=0; i<50; i++)
friendArr[i].name[0] = ''; // <<-- AND THE NAME


When you print out the friends list, the members names cannot be compared to NULL, since they are not pointers. (well they can be compared, but it does not make sense). It would be better, once the data-set is initialised to blank, to check to see if there's a name set.



for (int i = 0; i < 50; i++) 
{
if (friendArr[i].name[0] != '')
{
printf("%st%dn", friendArr[i].name, friendArr[i].number);
}
}


Also as @Groo points out, it's not valid to compare C strings with ==. Use strcmp() instead. Here we test that strcmp() returns 0 when the strings are identical.



for (int i = 0; i < 50; i++) 
{
if (strcmp(friendArr[i].name, name) == 0)
{
strcpy(friendArr[i].name, "");
}
}





share|improve this answer























  • Thank you so much.
    – Ervin Tan
    Nov 20 at 22:30











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',
autoActivateHeartbeat: false,
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%2f53402414%2fediting-array-of-struct-in-another-function%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









0














You need to initialise your variables:



void main () {

int index = 0; // <<-- THIS
int troll;

// Make some friends
friend friendArr[50];

for (int i=0; i<50; i++)
friendArr[i].name[0] = ''; // <<-- AND THE NAME


When you print out the friends list, the members names cannot be compared to NULL, since they are not pointers. (well they can be compared, but it does not make sense). It would be better, once the data-set is initialised to blank, to check to see if there's a name set.



for (int i = 0; i < 50; i++) 
{
if (friendArr[i].name[0] != '')
{
printf("%st%dn", friendArr[i].name, friendArr[i].number);
}
}


Also as @Groo points out, it's not valid to compare C strings with ==. Use strcmp() instead. Here we test that strcmp() returns 0 when the strings are identical.



for (int i = 0; i < 50; i++) 
{
if (strcmp(friendArr[i].name, name) == 0)
{
strcpy(friendArr[i].name, "");
}
}





share|improve this answer























  • Thank you so much.
    – Ervin Tan
    Nov 20 at 22:30
















0














You need to initialise your variables:



void main () {

int index = 0; // <<-- THIS
int troll;

// Make some friends
friend friendArr[50];

for (int i=0; i<50; i++)
friendArr[i].name[0] = ''; // <<-- AND THE NAME


When you print out the friends list, the members names cannot be compared to NULL, since they are not pointers. (well they can be compared, but it does not make sense). It would be better, once the data-set is initialised to blank, to check to see if there's a name set.



for (int i = 0; i < 50; i++) 
{
if (friendArr[i].name[0] != '')
{
printf("%st%dn", friendArr[i].name, friendArr[i].number);
}
}


Also as @Groo points out, it's not valid to compare C strings with ==. Use strcmp() instead. Here we test that strcmp() returns 0 when the strings are identical.



for (int i = 0; i < 50; i++) 
{
if (strcmp(friendArr[i].name, name) == 0)
{
strcpy(friendArr[i].name, "");
}
}





share|improve this answer























  • Thank you so much.
    – Ervin Tan
    Nov 20 at 22:30














0












0








0






You need to initialise your variables:



void main () {

int index = 0; // <<-- THIS
int troll;

// Make some friends
friend friendArr[50];

for (int i=0; i<50; i++)
friendArr[i].name[0] = ''; // <<-- AND THE NAME


When you print out the friends list, the members names cannot be compared to NULL, since they are not pointers. (well they can be compared, but it does not make sense). It would be better, once the data-set is initialised to blank, to check to see if there's a name set.



for (int i = 0; i < 50; i++) 
{
if (friendArr[i].name[0] != '')
{
printf("%st%dn", friendArr[i].name, friendArr[i].number);
}
}


Also as @Groo points out, it's not valid to compare C strings with ==. Use strcmp() instead. Here we test that strcmp() returns 0 when the strings are identical.



for (int i = 0; i < 50; i++) 
{
if (strcmp(friendArr[i].name, name) == 0)
{
strcpy(friendArr[i].name, "");
}
}





share|improve this answer














You need to initialise your variables:



void main () {

int index = 0; // <<-- THIS
int troll;

// Make some friends
friend friendArr[50];

for (int i=0; i<50; i++)
friendArr[i].name[0] = ''; // <<-- AND THE NAME


When you print out the friends list, the members names cannot be compared to NULL, since they are not pointers. (well they can be compared, but it does not make sense). It would be better, once the data-set is initialised to blank, to check to see if there's a name set.



for (int i = 0; i < 50; i++) 
{
if (friendArr[i].name[0] != '')
{
printf("%st%dn", friendArr[i].name, friendArr[i].number);
}
}


Also as @Groo points out, it's not valid to compare C strings with ==. Use strcmp() instead. Here we test that strcmp() returns 0 when the strings are identical.



for (int i = 0; i < 50; i++) 
{
if (strcmp(friendArr[i].name, name) == 0)
{
strcpy(friendArr[i].name, "");
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 22:30

























answered Nov 20 at 22:27









Kingsley

2,27711023




2,27711023












  • Thank you so much.
    – Ervin Tan
    Nov 20 at 22:30


















  • Thank you so much.
    – Ervin Tan
    Nov 20 at 22:30
















Thank you so much.
– Ervin Tan
Nov 20 at 22:30




Thank you so much.
– Ervin Tan
Nov 20 at 22:30


















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%2f53402414%2fediting-array-of-struct-in-another-function%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

Wiesbaden

To store a contact into the json file from server.js file using a class in NodeJS

Marschland