0

I'm trying to write a program that looks for the first empty space in a 2D array and adds a custom string to that space. I have tried some things that i found on the internet but none seem to work or match my specific scenario. This is it:

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

int tags[10] = {1,2,3,4,5};
char owners[10][10] = {"per1", "per2", "per3", "per4", "per5"};
int tagAdd;
char ownerAdd;
int i;
int addBool;
int j;
int len;

int main()
{
   printf("Enter the tag ID you want to add: ");
   scanf("%d", &tagAdd);
   printf("Enter the tag owners name: ");
   scanf("%d", &ownerAdd);
   len = strlen(ownerAdd);

   while (i<10)
   {
       if (tags[i] == 0)
       {
           tags[i] = tagAdd;
           owners[i][len] = ownerAdd; //This is the part I can't figure out
           addBool = 1;
       }
       if (addBool == 1)
       {
           break;
       }
       i++;
   }

   i = 0;
   addBool = 0;
   len = 0;

   while (i<10)
   {
       printf("tag[%d]", tags[i]);
       len = strlen(owners[i]);
       printf(" is owned by ");
       while (j < len)
           {
               printf("%c", owners[i][j]);
               j++;
           }
       printf("\n\r");
       i++;
       j = 0;
   }

}
2
  • Does this help; stackoverflow.com/questions/30774758/… Commented Feb 9, 2018 at 19:52
  • "first empty space in a 2D" is a bit unclear. A 2D array can not have any empty space. In this case, the 2D char owners[10][10] consists of 10*10 or 100 elements (char). Looks like this is more about char owners[10][10] as 10 strings and code is looking for the first string that begins with a null character. Commented Feb 9, 2018 at 20:42

1 Answer 1

0

You cannot do this:

char ownerAdd;

scanf("%d", &ownerAdd);
len = strlen(ownerAdd);

You are passing the incorrect types. ownerAdd is a single char, scanf expects with %d a pointer to int, you are passing a pointer to char and if scanf converts the value, it will overflow. And strlen expects a char* which points to a valid string (0-terminated). You are doing all this wrong.

This would be correct:

char ownerAdd[100];
scanf("%99s", ownerAdd);
len = strlen(ownerAdd);

And for replacing a value:

owners[i][len] = ownerAdd; //This is the part I can't figure out

is also wrong, because owners[i] is a char[10], you have to do:

strncpy(owners[i], ownerAdd, sizeof owners[i]);
owners[i][sizeof(owners[i]) - 1] = 0;

to copy the string.

The next error is that you don't initialize i and do (the first loop)

while (i<10)
{
    ...
}

this is going to fail. Same thing with j, it is uninitialized.

Sign up to request clarification or add additional context in comments.

1 Comment

There defenitely were alot of flaws in my code, thanks alot for helping me out here!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.