Brain Monkey
Brain Monkey

Reputation: 124

C - String arrays and functions

So I've been learning C and getting really confused on string arrays and pointers.
I am trying to pass in an array into buildArrays and populate it and use it back in main. When I print the array at the end of buildArrays it prints fine, but when I print it at the end of the main function, the index 0 string prints fine then the rest are off.

void buildArrays(char *fileName, char wordArray[][50], char gridArray[][50],int columnCounter, int rowCounter ) {

  FILE *dat2; // Create File objects for input and output file .
  dat2 = fopen(fileName, "r");

  char mystring[columnCounter * 3];
  int c, columnIndex = 0, rowIndex =0;
  while(fgets(mystring, (columnCounter * 3), dat2) != NULL) {

        // printf("Line: %s\n",mystring);
        // printf("rowIndex: %d - rowCounter: %d\n", rowIndex, rowCounter);
        // printf("...\n");

        if(rowIndex < rowCounter){
          strcpy(gridArray[rowIndex], mystring);
          //printf("GridArray: %s\n", gridArray[rowIndex]);
        }
      rowIndex++;
    }
     for(int b=0; b<rowCounter; b++)
   {
      printf("%s\n", gridArray[b]);
   }

}


int main(void)
{
  int rowCounter, columnCounter , wordCounter= 0;
  char fileName[100] = "data1";
  getArraySize(fileName, &columnCounter, &rowCounter, &wordCounter);
  printf("%d x %d", columnCounter, rowCounter);
  printf("Number of words: %d", wordCounter);
  printf("\n");

  char wordArray[wordCounter][columnCounter + 1];
  char gridArray[columnCounter][rowCounter * 2];

  buildArrays(fileName, wordArray, gridArray, columnCounter, rowCounter);

  for(int b=0; b<rowCounter; b++)
   {
      printf("%s\n", gridArray[b]);
   }



}

buildArrays print

S T E L B M T F E Y D E E P S R T C I A E E

N N E L I T R L B D E T A R E M U N E T Y L

N O I T A N I M I R C N I F L E S S E N T A

A U I D E W A R R A N T N U P R U S S E R P

P G S G E A L P A P B A N P S A S S N M E A

C O N S I T U T I O N D E E C W S O O H P D

S V W D E L A N E E J A M E S M A D I S O N

A E D E S N E G R J C U L T N O H L T I R A

A R C E R R T R E E S B O N E E I D N N P R

S N J U D I C I A L A S S E C O R P E U D I

S M R A R A E B W B E S S M E O A U V P E M

O E O I A I L N O U C D O D S S E N N I G R

L N I D G Y T R C O M P E N S A T I O N N D

D T O Z E H P Y N D R L E E A O H S C O I B

I T P S U E T G O L U Z M M R B E H P I R T

E O I E A R R S U U I B H A Y L L M S T F A

R I N R E E E F U T L V Q U A R T E R I N G

S I D B S R R D I Y E N I G M I A N A T I R

S Q I S E B S C N S P E E C H R O T A E Y N

D L C M I L I T I A F L R N C A T S S P S E

R U T E D Y L E B I L C O H M L E T E S Y Y

L S T R T E W Z L I O S A E N S A E I Y A L

main print

S T E L B M T F E Y D E E P S R T C I A E E

T Y L

S S E N T A

U P R U S S E R P

B A N P S A S S N M E A

T I O N D E E C W S O O H P D

E L A N E E J A M E S M A D I S O N

E D E S N E G R J C U L T N O H L T I R A


P R

P E U D I

E O A U V P E M

D O D S S E N N I G R

C O M P E N S A T I O N N D

H P Y N D R L E E A O H S C O I B

P S U E T G O L U Z M M R B E H P I R T


A

R I N G

I A N A T I R

Upvotes: 0

Views: 63

Answers (2)

ShadowRanger
ShadowRanger

Reputation: 155323

Your definition of the arrays in main doesn't match what buildArrays expects. The second dimension in main is dynamic, and usually not 50, but buildArrays always assumes it's 50. That's why your results are screwy; gridArray[b] refers to a place in memory b * rowCounter * 2 bytes past the first byte of gridArray in main, but it refers to memory b * 50 bytes past the first byte in buildArrays.

You see it misbehave only on the second and subsequent values of b (because b of 0 will multiply to 0 all the time). But when b is 1, buildArrays is filling in bytes 50-100 (although it actually leaves most of them as NULs), while main is reading from byte rowCounter * 2 until it hits a NUL byte. For b of 1, I'm guessing it tried to look in a spot that began with a NUL (thus the lack of output), but eventually some b * rowCounter * 2 wraps back into non-NUL text, skipping past the:

N N E L I T R L B D E T A R E M U N E

part of:

N N E L I T R L B D E T A R E M U N E T Y L

and only prints the final:

T Y L

before it sees a NULL and stops.

Point is, your second and subsequent dimensions of a multidimensional array matter; the first dimension doesn't (a function just receives it as a pointer anyway), but all subsequent dimensions must be known to determine how to interpret indexing of that pointer.

Upvotes: 2

David Schwartz
David Schwartz

Reputation: 182753

void buildArrays(char *fileName, char wordArray[][50], char gridArray[][50],
    int columnCounter, int rowCounter ) {

...

char wordArray[wordCounter][columnCounter + 1];
char gridArray[columnCounter][rowCounter * 2];

Unless columnCounter + 1 is guaranteed to be 50 and rowCounter * 2 is also guaranteed to be 50, the two functions will have different understandings of the memory layouts of wordArray and gridArray. So one function will not be storing the strings where the other function expects to find them.

Upvotes: 2

Related Questions