0

I want my loop to repeat only as many times, as many are strings in "fruits" array.

Tried it first by using "while" loop, but I couldn't make it to work properly (now it is commented), because (as debugger showed) it segfaulted on 4th iteration. This method should work in array of chars or integers but does not work in array of strings. Why is it like this?

Then I tried to use "for" loop and wanted it to stop when reaching total number of string elements in array, stored in "count" variable. However, I could not find a good method to count number of strings in array. Is that even possible? "sizeof" operator doesn't seem like a good solution here.

Thanks in advance for response.

#include <stdio.h>

int main()
{
    char *fruits[]={"Apple", "Grapefruit", "Banana"};
    int i=0;
    int count=sizeof(*fruits);


    char **bitter=&fruits[1];

    printf("Bitter fruit is: %s", *bitter);

    puts(" ");

    printf("All fruits are: ");

    for (;i<count;i++);
    {
        printf("%s ",*(fruits+i));
    }

    /*
        while ( fruits[i] != '\0')
        {
        printf("%s ",*(fruits+i));
        }

    stuff above failed. why?
    */


    return 0;
}
1
  • Is the stuff with using for loop and counting array string elements even possible? Commented Aug 26, 2012 at 9:35

3 Answers 3

4

An easy way would be to append a NULL to your list:

char *fruits[] = {"Apple", "Grapefruit", "Banana", NULL};

Then you can print all fruits like this:

int i = 0;
while (fruits[i] != NULL) {
    printf ("%s ",*(fruits+i));
    i++;
}

As to why your original while loop failed:

  • your i doesn't get incremented
  • you are checking if fruits[i] != '\0' where fruits[i] is a pointer to char and '\0' is a char equal to 0. So you are essentially checking if fruits[i] points to 0 but for the first three iterations this is not the case as fruits[i] points to the first character of the respective fruit and in the fourth iteration fruits[i] points to a memory location that doesn't belong to your program.
Sign up to request clarification or add additional context in comments.

2 Comments

Good, but it's probably cleaner to write fruits[i] rather than *(fruits+i).
Thanks @eyelash. @PaulR *(fruits+i) is there just because I am recently trying to get familiar with pointers.
0
#include <stdio.h>
int main()
{
    char *fruits[]={"Apple", "Grapefruit", "Banana"};
    int i=0;
    int count=sizeof(fruits)/sizeof(fruits[0]);
    char **bitter=&fruits[1];

    printf("Bitter fruit is: %s\n", *bitter);
    printf("All fruits are:\n");
    for(i=0;i<count;i++)
    {
        printf("%s\n",fruits[i]);
    }
    return 0;
}

1 Comment

This doesn't work because (sizeof(fruits[0])) is smaller than sizeof(fruits[1]), etc... So there is no uniform size. Your approach would work with an array of fixed size elements, such as int numbers[], etc...
-1

You can also consider using the strlen() from header string.h :

#include <stdio.h>
#include <string.h>
int main()
{
    char *myarray[] = {"Mere", "Pere", "Gutui"};
    int i = 0;
    int lungime = strlen(*myarray);

    while (i < (lungime - 1)){
            printf("Fruit no. %d is %s\n", i, *(myarray + i));
            i++;
    }
    return 0;
}

1 Comment

-1 This is completely wrong. You are using length of the string "Mere" as a length of myarray. strlen does not return length of an array. It returns length of nul terminated string.

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.