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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:15:06+00:00 2026-06-12T03:15:06+00:00

I’ve seen this question and I noticed that I get errors when I declare

  • 0

I’ve seen this question and I noticed that I get errors when I declare variables in the middle of the main() function, but I thought that creating variables dynamically wouldn’t cause an error, because it can be done anywhere and anytime during run-time (as far as I know).

However, I still get:

error C2065: 'i' : undeclared identifier
error C2065: 'z' : undeclared identifier
error C2065: 'intArr' : undeclared identifier

My code:

int main()
{
  ..... 
  .....
  ..... 

  printf("Type the array size:\t");
  int *z = (int *)malloc(sizeof(int));
  scanf("%d", z);
  int *intArr = (int *)malloc((*z) * sizeof(int));

  int *i = (int *)malloc(sizeof(int));

  for (*i = 0; *i < *z; ((*i)++))
  {
      printf("Type a number\t");
      scanf("%d", (intArr+(*i)));
  }

  printArr(intArr);
}

void printArr(int *arr)
{
    int i; 
    for (i = 0; i < (sizeof(arr) / sizeof(*arr)); ++i)
        printf("%d ", *(arr+i));
}
  • 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-12T03:15:08+00:00Added an answer on June 12, 2026 at 3:15 am

    (I’m not sure why @Blood deleted his answer; it was essentially correct.)

    When I compile your program using gcc, it compiles with no errors. I had to add

     #include <stdio.h>
     #include <stdlib.h>
    

    to the top, and delete the three ..... lines.

    When I compile the same program using Microsoft’s Visual C++ 2010 Express, I get a number of errors. Several of them complain about undeclared identifiers, but that’s a common side effect of syntax errors; if the compiler can’t parse your source file, it’s likely to become “confused” as it tries to recover. The most relevant error is:

    syntax error : missing ';' before 'type'
    

    on line 10:

    printf("Type the array size:\t");     // line 9
    int *z = (int *)malloc(sizeof(int));  // line 10
    

    The problem is that the 1989/1990 version of the C standard doesn’t permit mixing declarations and statements within a block; it requires all declarations to appear first, followed by all statements. The 1999 C standard changed that, but Microsoft’s C compiler has very limited support for any C standard past the 1990 one (and they’ve said they have no intention of changing that). (I expect they might permit mixed declarations and statements in a future version, since that’s a C++ feature as well.)

    (I’m assuming from the form of the error messages that you’re using a Microsoft compiler.)

    You can rearrange your code to satisfy the Microsoft compiler’s restrictions. In some cases, you might need to change something like

    int *var = initial_value;
    

    to

    int *var;
    // ...
    var = initial_value;
    

    Another suggestion, not related to your question:

    In C, you shouldn’t cast the result of malloc(). The malloc() function returns a value of type void*, which can be implicitly converted to any pointer-to-object type. (C++ doesn’t have this implicit conversion, but you probably shouldn’t be using malloc() in C++ anyway.)

    Rather than this:

    int *z = (int *)malloc(sizeof(int));
    ...
    int *intArr = (int *)malloc((*z) * sizeof(int));
    

    you can write this:

    int *z = malloc(sizeof *z);
    ...
    int *intArr = malloc(*z * sizeof *intArr);
    

    Dropping the unnecessary cast can avoid certain errors; for example, with some compilers a cast can mask a necessary error message if you’ve forgotten the required #include <stdlib.h>. And applying sizeof to *z or *intArr, rather than naming the size explicitly, means that you won’t have to change the call if the type of the pointer changes. If you write, for example:

    double *p = malloc(sizeof (int)); // incorrect
    

    then you’re allocating the wrong size, but the compiler won’t warn you about it.

    Also, if you’re allocating a single int value using malloc(), as you’re doing with your i and z pointers, you’re doing unnecessary work. Unless your purpose is to practice using malloc(), you might as well just make i and z int variables, and drop the malloc() calls. You’ll just have to pass their addresses to scanf. In other words, you can change this:

    int *z = (int *)malloc(sizeof(int));
    ...
    scanf("%d", z);
    

    to this:

    int z;
    ...
    scanf("%d", &z);
    

    One more point: your program has no error checking. malloc() can fail if there isn’t enough memory to allocate; it returns a null pointer (NULL) when this happens. scanf() can fail if there’s an input error, or if you type hello when it’s expecting to read an int. scanf() returns the number of items it successfully scanned; you should verify that it did so (in this case, it returns 1 on success). For a simple program like this, aborting the program with an error message:

    fprintf(stderr, "Call to ... failed\n");
    exit(EXIT_FAILURE);
    

    is probably good enough.

    • 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
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a French site that I want to parse, but am running into
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.