Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8431773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:56:45+00:00 2026-06-10T05:56:45+00:00

I want my loop to repeat only as many times, as many are strings

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:56:47+00:00Added an answer on June 10, 2026 at 5:56 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the repeat tag of JSF 2.0 to loop through a list of
I want a GridView in Android to loop endlessly / repeat all items when
I want to loop over a range of numbers from 1:n , when n
i want to loop trough all my delicious.com bookmarks and wrap a link arround
I want to loop through the checkboxgroup 'locationthemes' and build a string with all
I want to loop true certain elements, in this case all with the class
I want to loop through the controls on a UserControl and set a property
I want to loop through a database of documents and calculate a pairwise comparison
I want to loop over a series of files in a directory in batches
I want to loop over the files that begin with '.' in directory x

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.