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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:34:48+00:00 2026-06-04T20:34:48+00:00

I am reading C Primer Plus these days and here is the code I

  • 0

I am reading C Primer Plus these days and here is the code I wrote for the programming practice No.4 in Chapter 10, finding the index of the largest number in a double-typed array. I used variable length array in order to manually specify the array size:

#include <stdio.h>
int findmax(const double array[], int s);
//find the index of the largest number in the array
int main(void)
{
    int size = 0; //size of the array
    int index = 0; //index of the largest number
    double num[size]; //the array holding double-type numbers

    printf("Enter the size of the array: ");
        scanf("%d", &size);
    printf("Enter %d numbers: ", size);
    for (int i = 0; i < size; i++)
        scanf("%lf", &num[i]);

    index = findmax(num, size);
    printf("The index of the max number in the array is: %d\n", index);
    return 0;
}

int findmax(const double array[], int s)
{
    int index = 0;
    double max = array[0];
    for (int i = 0; i < s; i++)
            if (array[i] > max)
            {
                max = array[i];
                index = i;
            }
    return index;
}

This piece of program compiles normally, using MinGW (assume the program file name is prog.c):

gcc prog.c -o prog.exe -std=c99

The program works fine when the “size” varialbe is less than 5. But when I enter 6 or larger numbers for the “size” varialbe, the program crashes during runtime.

Loosely translated, the error message is:

the memory 0x00000038 used by 0x77c1c192 could not be "written".

I tried to eliminate the use of variable length array, the program seems to work fine. But I still couldn’t get where is wrong with the original one.

  • 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-04T20:34:50+00:00Added an answer on June 4, 2026 at 8:34 pm

    Size is 0 when you allocate num. You get access violation later on because you try to acces num[0] which has not been allocated.

    EDIT: I propose to use dynamic memory or declare num after size is read.

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

Sidebar

Related Questions

While reading C++ Primer Plus 5th edition, I saw this piece of code: cin.get(ch);
I am reading the chapter function templates of C++ Primer 3rd edition , and
I was reading through the c++ Primer and this code snippet came up and
I am reading the book C++ Primer and at the file input output chapter
Im reading c++ primer plus and having some issues understanding how implicit instantiation works.
I am reading the C++ Primer, in the overloaded operation chapter, the author gave
I'm reading a book, the Primer guide to C++. I think I got the
Reading the code of many javascript libraries, I see that many developers use to
Reading the design guidelines. I came across a little issue while trying to practice
Reading some questions here on SO about conversion operators and constructors got me thinking

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.