1

I am trying to read a file and store only the digits in the array.

My Example is file.csv

"1","2","3" "1","4","6","7" "12","15"

Please do not suggest these solutions

sizeof(arr) / sizeof(arr[0]) or *(&arr + 1) - arr

because both of these don't give the length of array containing actual values. It just gives the length of array when array was declared. I want to know the length of array during run-time when actual values are present.

The Problem Statement: Find the numOfwords in each line (present in code comments).

My code is below:

#include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> int main(int argc, char *argv[]) { int i = 0, j = 0, k = 0; char literal, string[4][10][4]; char *filename = "file.csv"; FILE *f; f = fopen(filename, "r"); while ((literal = getc(f)) != EOF) { if (isdigit(literal)) { string[i][j][k] = literal; k++; } else { if (k > 0) { string[i][j][k] = '\0'; } k = 0; } if (literal == ',') j++; if (literal == '\n') i++, j = 0; } int numOfLines = i; int total = 0; for (int p = 0; p < numOfLines; p++) { // int numOfWords = *(&string[p] + 1) - string[p]; // Returns 10 // int numOfWords = sizeof(string[p]) / sizeof(string[p][0]); // Returns 10 // // The Problem Statement: Find the numOfwords in each line. printf("numOfWords in line %p: %d\n", numOfWords); for (long int q = 0; q < numOfWords; q++) { /* code */ total += atoi(string[p][q]); } } printf("Sum = %d", total); } 

Any solution better than this code is appreciated.

Output:

numOfWords in line 1: 3 numOfWords in line 2: 4 numOfWords in line 3: 2 Sum = 51

8
  • 1
    sounds like a job for codereview.stackexchange.com Commented Aug 3, 2018 at 20:59
  • I don't get the problem. You read something, then you store it in the array. So count each time you are doing this... Commented Aug 3, 2018 at 21:00
  • 2
    The size of an array has nothing to do with the presence of "actual values," but only with the size of the declared array. The sizeof operator yields this value at compile time unless the operand is a VLA, in which case it is evaluated at runtime. But if you want to know how many "meaningful" values an array contains, maybe you want to include a sentinel value. Commented Aug 3, 2018 at 21:09
  • 1
    char literal; ... while ((literal = getc(f)) != EOF) { is an infinite loop when char is unsigned and insufficient when char is signed and (char)EOF is read. Use int literal; Commented Aug 4, 2018 at 4:15
  • 1
    An array size is constant in C, once defined. char literal, string[4][10][4]; is always a 4x10x4 array of char. Content is irrelevant. Commented Aug 4, 2018 at 4:19

2 Answers 2

2

You have to keep track on the number of elements you have placed in the array yourself. There is no other way.

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

1 Comment

Expanding on this, you can either cache those values when you read them in, saving the maximum value of j for each i, or you can evaluate at runtime by scanning the appropriate array and counting valid elements.
-1

maybe:

typedef struct { size_t size; size_t elementsize; unsigned char array[]; }myarray; myarray *MA_Init(void) { myarray *arr = calloc(1, sizeof(*arr)); return arr; } myarray *MA_alloc(myarray *arr, size_t nelem, size_t elemsize) { narr = realloc(arr, nelem * elemsize + sizeof(*narr)); if(narray) { narray -> size = nelem; narray -> elementsize = elemsize; } return narr; } int MA_Set(myarray *arr, size_t index, void *elem) { int result = !arr; result = result && (index < arr -> size); if(result) { memcpy(&arr -> array[index * arr -> elementsize], elem, arr -> elementsize); } return result; } int MA_Get(myarray *arr, size_t index, void *elem) { int result = !arr; result = result && (index < arr -> size); if(result) { memcpy(elem, &arr -> array[index * arr -> elementsize], arr -> elementsize); } return result; } 

... and so on....

2 Comments

DV and no comment?
No downvote by me, but I was tempted by your explanation and the assertiveness of your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.