-1

Is there a way, I can use of strlen() to find the length of the arrays, instead of specifying in the loop. First Code: Working With x < 4 Which is the size of the array of temps[4].

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

int main(){
    float temps[4] = {72.5, 73.4, 74.7, 75.2};
    int x;
    printf("Local Temperatures\n");
    for(x = 1; x < 4; x++){
        printf("Station %d: %.1f\n",x,temps[x]);
    }
}

My Second Code, Not Working, But Looking At What, I Am Trying To achieve with strlen() to find the size of the array.:

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

int main(){
    float temps[4] = {72.5, 73.4, 74.7, 75.2};
    int x;
    float size;
    size = temps;
    printf("Local Temperatures\n");
    for(x = 1; x < strlen(size); x++){
        printf("Station %d: %.1f\n",x,size[x]);
    }
}
  • 3
    `strlen` is for strings. Why do you think it would work for any other array type? To get the number of element of an array do `sizeof temps / sizeof temps[0]`. – kaylum Jan 02 '20 at 10:59
  • 1
    What does your compiler put out if you enable all compiler warnings? You need to do that. What you are trying to do is unclear, and it's not proper C code. – Andrew Henle Jan 02 '20 at 11:01
  • 1
    1. `strlen` is only for char* you can't use it for array. 2. temps can be indexed only from 0 to 3, array out of bound access is happening in your snippet – TruthSeeker Jan 02 '20 at 11:02
  • Tip: `float temps[] = ...;` is better than `float temps[4] = ...;` – ikegami Jan 02 '20 at 11:09

5 Answers5

3

strlen is effectively an optimized version of the following:

size_t len = 0;
const char *p = s;
while (!*p) {
   ++len;
   ++p;
}

You can easily adapt that.

size_t len = 0;
const float *p = s;
while (*p != 0.0) {
   ++len;
   ++p;
}

Of course, that means you need a sentinel value just like you had for the string.

float temps[] = { 72.5, 73.4, 74.7, 75.2, 0.0 };

While you can use a value other than 0.0 as your sentinel value, using a sentinel value might not be desirable, so you might not really want to take the same approach as one does for strings.

For an array (as opposed to a pointer), you can use the following to determine the number of elements in the array:

sizeof(a)/sizeof(*a)

That means you can use the following to determine the number of elements in temps:

sizeof(temps)/sizeof(*temps)
ikegami
  • 367,544
  • 15
  • 269
  • 518
1

Simple method is to get the size of a array is by sizeof(temps)/sizeof(temps[0])

something like,

for(x = 0; x < sizeof(temps)/sizeof(temps[0]); x++){
        printf("Station %d: %.1f\n",x,temps[x]);
    }

Note: In your snippet array access is done from 1 to (size) instead 0 to (size-1) which is out of bound access.

TruthSeeker
  • 1,539
  • 11
  • 24
  • When you tell people about using `sizeof x / sizeof *x` to get the size of an array, advise them it works only for arrays, not for pointers, to ward off the mistake made much too frequently by students. – Eric Postpischil Jan 02 '20 at 12:46
1

Just do this to get the length of your float array,

int main()
{
    float ar[4] = {1.1,1.2,1.3,1.4};
    float b;
    printf("Array Size : %d\n",sizeof(ar)/sizeof(b));
    return 0;
}

You'll get the total number of element of your array , which you can use further in a loop to print the elements of array. Note: Do not Use sizeof in case of pointers.

M.Shah
  • 111
  • 1
  • 9
  • 1
    When you tell people about using `sizeof array / sizeof element` to get the size of an array, advise them it works only for arrays, not for pointers, to ward off the mistake made much too frequently by students. – Eric Postpischil Jan 02 '20 at 12:46
1

Short answer: no.

strlen expects an argument of type char *, which points to the first character of a string, which is a sequence of character values including a zero-valued terminator. strlen returns the number of characters before the terminator:

/**
 * A naive implementation of strlen.  Actual implementations
 * are a little more clever.
 */
size_t strlen( const char *str )
{
  size_t count = 0;
  while( str[count] )
   count++; 
 return count;
}

The important thing to remember is that strlen returns the length of the string, not the size of the array containing the string. If you have something like

char foo[1024] = “bar”;

the strlen( foo ); returns 3.

If you tried to pass an integer or floating point array to strlen the compiler would yell at you because of the wrong argument type. You could work around this by casting the argument to char *, but you would still likely get a wrong answer, not only because integer and float types are multiple chars wide, but also because such a value may have an embedded zero-valued byte. For example, the integer value 16 is represented as the bytes 0x00, 0x00, 0x00, 0x10. If that’s the first element of your integer array, then strlen would return 0 on a big-endian platform and 1 on a little-endian platform.

If you defined the array, then you know how big it is. If you’re writing a function that receives an array argument, then you must either also receive the size as a separate argument, or you must rely on the presence of a sentinel value in the array.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

strlen() takes a "const char *" type value as argument. So it cannot be used with integer or float arrays.

dev7060
  • 120
  • 8