0

I'm currently having problems passing character arrays from my main function to some function that counts the number of elements in my array.

I ask for 2 separate string characters using the getchar() function.

To clarify, here is a snippet of my code:

I've tried using scanf to &myArray[0] as an alternative <-- is this okay? Given that the user inputs for example 5 characters, does the program automatically increment to &myArray[++] for each succeeding character?

#include <stdio.h>
#include <stdlib.h>
#define ARRAY_LEN 20


int match(char s[], char t[] )
{

    int i = 0;
    int j = 0;

    printf("begin: ivaL: %d, jval: %d\n", i, j);
    while(s[i] != '\0')
        i++;

    while(t[j] != '\0')
        j++;

    printf("i val: %d, jval: %d ", i, j); /*J value is messed up, it prints 20+ when ive only typed 5 characters or so*/


}

int main()
{
    int cur = 0;
    char char1[ARRAY_LEN];
    char char2[ARRAY_LEN];
    char c;
    char f;
    printf("Enter char: ");
    while((c=getchar())!=EOF && c!='\n')
    {
        char1[cur++] = c;
    }
    cur = 0;
    printf("\n2nd Char: ");
    while((f=getchar())!=EOF && f!='\n')
    {
        char2[cur++] = f;
        putchar(f);
    }
    putchar('\n')
    printf("Cur value:  %d\n", cur); //here cur accurately prints the correct number of while loops done when counting the characters

    match(char1, char2); /*here i tried to fetch the number of elements in char2 before i passed it to match function and in here char2 is showing correct value, something happens when i pass char2 to the match function*/



}
jdoecs420
  • 35
  • 6

2 Answers2

1

You've syntax errors to sort out :

putchar('\n') // Missing semicolon.

Null characters are not added by default after you read a set of characters.

while((c=getchar())!='\n')
    {
        char1[cur++] = c;
    }
char1[cur] = '\0'; // Adding a null terminator to make the identifier a C Style string.

And similarly for the second.

Apart from that, you have other issues.

int match(char s[], char t[] )

should return an integer. You might consider doing something like :

return i==j;

Catch all the compiler warnings (say use -Wall in gcc)

I may rewrite that piece of code like below ::

#include <stdio.h>

#define ARRAY_LEN 30

int match(char * restrict str1,char * restrict str2)
// If str1 and str2 is is the sole agencies dealing with the strings
// then 'restrict' helps compiler with some optimizations.
{
size_t count1=0,count2=0;
while(str1[count1++] != '\0')
  ;;
while(str2[count2++] != '\0')
  ;;
return count1==count2;
// Ideally  count1-1 == count2 -1
// But does that make any difference?
}

int main(int argc,char * argv[])
{
  char str1[ARRAY_LEN];
  char str2[ARRAY_LEN]; // No harm doing so.
  signed x; // If you want to check against EOF
  int count=0;
  while((x=getchar()) != '\n' && x!=EOF )
    // You need to implement bounds check.
    {
      if(count < (ARRAY_LEN - 1))
      {
        str1[count++]=x;
      }
      else
      {
        // C input is buffered
        // so you need to clear the buffer if the string first entered was larger than 30 characters
        while((x=getchar()) != '\n' && x!=EOF )
          ;;
        break;
      }
    }
  // C input is buffered
  // so you need to clear the buffer if the string first entered was larger than 30 characters


  str1[count] = '\0' ; // Null terminating
  count = 0; // Reset count

while((x=getchar()) != '\n' && x!=EOF )
    // You need to implement bounds check.
    {
      if(count < (ARRAY_LEN - 1))
      {
        str2[count++]=x;
      }
      else
      {
        // C input is buffered
        // so you need to clear the buffer if the string first entered was larger than 30 characters
        while((x=getchar()) != '\n' && x!=EOF )
          ;;
        break;
      }
    }
  str2[count] = '\0' ; // Null terminating
  printf("Arrays are of %s length\n",match(str1,str2)?"same":"different");
  return 0;
}

Edit: The EOF macro is defined as -1. To accommodate that x needs to be a signed integer. Read this answer inline with this answer.

sjsam
  • 21,411
  • 5
  • 55
  • 102
  • `EOF` isn't a character, so that loop condition has implementation dependent behavior. – Pierce Griffiths Apr 29 '19 at 03:41
  • 1
    @PierceGriffiths That's right, I have already mentioned that in the question [comment](https://stackoverflow.com/questions/55896555/character-arrays-passing-into-functions/55896635#comment98449445_55896555). Changed thanks. – sjsam Apr 29 '19 at 03:43
1

Your match() function iterates through your char arrays until it finds a null terminator, but you never actually put a null terminator anywhere in the arrays.

printf("Enter char: ");    
while((c=getchar()) !='\n'){
    char1[cur++] = c;
}
char1[cur] = '\0';
cur = 0;
printf("\n2nd Char: ");    
while((f=getchar()) !='\n'){
    char2[cur++] = f;
}
char2[cur] = '\0';
Pierce Griffiths
  • 733
  • 3
  • 15