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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:05:26+00:00 2026-06-09T20:05:26+00:00

I want to check an array in a condition. Let’s take this simple code

  • 0

I want to check an array in a condition. Let’s take this simple code below:

#include <stdio.h>
int main()
{
    int array[] = {1,2,3,4,5}; // initializing an array
    if(array[] == {1,2,3,4,5}) // using as condition
    {
         printf("worked");
    }
    else printf("not worked");

    return 0;
}

But it gives an error:

In function 'main':|
C:\Python32\Untitled4.c|5|error: expected expression before ']' token|
||=== Build finished: 1 errors, 0 warnings ===|

So how should I use an array in a condition?

  • 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-09T20:05:27+00:00Added an answer on June 9, 2026 at 8:05 pm

    If you have a modern C compiler, at least C99, you can use a compound literal and a function to do that comparison:

    if(memcmp(array, (int[]){1,2,3,4,5}, sizeof array) == 0) {
       printf("worked");
    }
    
    • Here memcmp (memory compare) compares the data to which the two
      pointers point.
    • (int[]){1,2,3,4,5} is the compound literal, something with the type in () and then an initializer in { } as you have in the declaration of your variable.
    • the two arrays are converted to pointers to their first elements when used like in expressions like that

    Edit: As Eric correctly remarks, memcmp is only a valid comparison if the base type of your array (here this is int) has no padding bits or bytes. For int this is uncommon these days, so what I describe is fine on usual platforms. If you’d have other, more complex, data types some day, you would have to write your own comparison function for arrays of that type.

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

Sidebar

Related Questions

I want to check to see if an array has - to activate a
I have an array I want to check the the last digits if it
I want to check if a value exists in an array made from a
I want to check checkbox like $('#riderCheck').attr('checked','true') on some condition. but the problem is
I want to check whether any item from the array is part of a
I have a randomly generated 10x5 array that I want to check when 3
I have an array as arr = [1,2,3,4,6,7,8,9] Now I want to check if
I want to check if my array is empty or null, and on base
I want to pass arrays in the jquery $.post() request. HTML: <input type=checkbox id=add-check-1e30>
I don't want check if an item with a value exists in the arraylist,

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.