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

  • Home
  • SEARCH
  • 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 6960227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:22:30+00:00 2026-05-27T15:22:30+00:00

I am trying to understand why my code crashes. I have an array of

  • 0

I am trying to understand why my code crashes. I have an array of structs which look like this:

typedef struct contact {

    char cFirstName[10];
    char cLastName[10];
    char cTelphone[12];

} address ; // end type

In the code I initialize the array like this:

address myContacts[5];

for ( i = 0; i < 5 ; i++ ){
        strcpy(myContacts[i].cFirstName, "0");
        strcpy(myContacts[i].cLastName,"0");
        strcpy(myContacts[i].cTelphone,"0"); 
    }

This works:

for ( i = 0; strcmp(myContacts[i].cFirstName,"0") != 0 ; i++ ){                                             
        printf("\nmyContacts[%d].cFirstName: %s", i, \
        myContacts[i].cFirstName );
    }// end for

So, I only print out Contacts which have content.

However, I can’t under stand why my search contact function does not work:

void searchContact( address * myContacts,    char * name ){
    int found = 1;
    int i = 0;

    for ( i = 1; found != 0 ;i++ ){
    found=strcmp(myContacts[i-1].cFirstName, name);

    printf(" Name Found %s",   myContacts[i-1].cFirstName);
    }
} // end of searchContacts

I call this function like this:

printf("\nEnter a name or part of a name to search:\n");
            fscanf(stdin, "%s", buffer);
            getchar(); // clear the last enter
            printf("\nThe line you entered was:\n");
            printf("%s\n", buffer);
            searchContact( myContacts, buffer );

If I search for an existing name it is found, and everything is OK. However, searching for
a non existing name, causes a segmentation fault.
Is there an obvious thing I am missing here ?

  • 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-05-27T15:22:31+00:00Added an answer on May 27, 2026 at 3:22 pm

    The probelm is here:

            for ( i = 1; found != 0 ;i++ ){
            found=strcmp(myContacts[i-1].cFirstName, name);
    
            printf(" Name Found %s",   myContacts[i-1].cFirstName);
            }
    

    You need to add something like for ( i = 1; found != 0 && i < num_of_contacts ;i++ )
    otherwise you going out of your array!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand this inline assembly code which comes from _hypercall0 here
I am now trying to understand some code and I have found a pattern,
I'm trying to understand this bit of code: in display.php: <html> ... <body> <table>
Trying to understand Ruby a bit better, I ran into this code surfing the
I am trying to understand this simple hashlib code in Python that has been
I have a maven project imported into Eclipse. I'm trying to understand the code
I trying to understand this piece of code Function.prototype.method = function (name, fn) {
just trying to understand this code. $db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
I am trying to understand the code in this page: https://github.com/corroded/git-achievements/blob/gh-pages/git-achievements and I'm kinda
I am trying to understand this code: d=edge(d,'canny',.6); figure, imshow(d,[]) ds = bwareaopen(d,40); figure,

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.