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 8627505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:17:49+00:00 2026-06-12T08:17:49+00:00

I’ve been experimenting with C a little bit. I usually use PHP and javascript.

  • 0

I’ve been experimenting with C a little bit. I usually use PHP and javascript.

I did “Hello World” and then I typed in this, which I copied from a website somewhere…

#include <stdio.h>
#include <stdlib.h>
#define MAX 20
int intcmp(const void*v1, const void *v2){
    return (*(int *)v1 - *(int *)v2);
}
main(){
    int arr[MAX], count, key , *ptr;
    printf("Enter %d integer values; press enter after each\n", MAX);
    for (count = 0; count < MAX; count++)
        scanf("%d", &arr[count]);
    puts("Press a key to sort the values");
    getc(stdin);

    qsort(arr, MAX, sizeof(arr[0]), intcmp);

    for(count=0; count < MAX; count++)
        printf("\narr[%d] = %d.", count, arr[count]);

    puts("\nPress a key to continue");
    getc(stdin);

    printf("Enter a val to search for");
    scanf("%d", &key);

    ptr = (int * )bsearch(&key, arr, MAX, sizeof(arr[0]), intcmp);

    if(ptr != NULL){
        int fred =  (ptr - arr);
        printf("%d found at arr[%d]", key ,fred);
    }else{
        printf("%d not found", key);
    }
}

So far so good. I’m trying to understand what all the stars do, but it’s falling into place (ha ha – falling stars 🙂

However, if I type in a float e.g. 21.45 when it asks me for 20 integers, it rushes through to “Enter a val to search for” having filled the 20 array values with weird numbers.

Have I created some sort of buffer overflow? I realise that input should be checked – but I’m interested to know what I’ve done. Could I use my program to run arbitrary code? (Well, no, not with my knowledge… but could someone?)

  • 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-12T08:17:50+00:00Added an answer on June 12, 2026 at 8:17 am

    However, if I type in a float e.g. 21.45 when it asks me for 20 integers, it rushes through to “Enter a val to search for” having filled the 20 array values with weird numbers

    If you enter a value such as 21.45 the call to scanf("%d") will fail, as it is not an int, and will leave the .45 in stdin (the 21 will be extracted as a valid int) to be processed again. This causes the loop to re-read this value again and again (as it fails every time). The weird numbers are due to the elements of the array being uninitialised.

    Check the return value of scanf() which returns the number of assignments made and if it fails skip whatever is in stdin:

    int count = 0;
    while (count < MAX)
    {
        if (1 == scanf("%d", &arr[count]))
        {
            count++; /* OK, so get next. */
        }
        else
        {
            /* !OK, skip invalid input. */
            int c;
            while ((c = fgetc(stdin)) != EOF && c != '\n');
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.