- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- About the company

Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
initializing array element to NULL [closed]
How to initialise an element of array to NULL.For example if I have char *array[10]; I want last element to be NULL, so that I can pass this array to execv
- 5 Just set it to 0. NULL should be used with pointers. – chris Nov 8, 2012 at 16:27
- sorry I edited. it meant to be pointer – Nazerke Nov 8, 2012 at 16:28
- @chris: the part about execv suggests that it should actually be NULL , but the OP doesn't realize that they're handling an array of pointers. – Fred Foo Nov 8, 2012 at 16:29
- 1 @chris: you're both right, execv takes a const char* as well as a char** . – Steve Jessop Nov 8, 2012 at 16:30
- 1 To do literally what you asked, set the last element of a 10-element array to a null pointer, array[9] = 0; . But I suspect that's not all there is to it. – Steve Jessop Nov 8, 2012 at 16:50
4 Answers 4
To initialise an array of char* to all NULL s:
If you want to provide initial elements for execv() :
or if you wish to omit the dimension from the array declaration:
NULL is nothing but : #define NULL (void*) 0 UL The NULL you are talking about is nul character which is '\0'
see man execv page or other exec processes .. it's actually take variable number of arguments
- ..and what ascii value does the '\0' have? – duedl0r Nov 8, 2012 at 16:36
- @duedl0r That would be 0 – WhozCraig Nov 8, 2012 at 16:38
- It's not standard but you can say null There are 3 types, 1st is : NULL pointer already know, 2nd is NUL character which is '\0' it's ASCII is 0. And the 3rd is actual zero which is decimal 0 having ASCII 48 – Omkant Nov 8, 2012 at 16:44
- @dueld0r: since character constants in C have type int , the character constant '\0' means exactly the same thing as the integer constant 0 . Some people prefer it, because it reminds them that they're using characters. Personally I can't help but read it as "the int value of the character whose integer value is 0" (in C++ it's, "the char value of the character whose integer value is 0" and means the same as char(0) ). – Steve Jessop Nov 8, 2012 at 16:51
- @Omkant Although it seems obvious what you are trying to express, its formally not correct: NUL / '\0' is the ASCII character which is represented by the decimal value 0 . There is no ASCII character 0 . Decimal 48 being ASCII character '0' is a different story. – alk Nov 8, 2012 at 16:57
execv takes an array of char * , not an array of char .
IF your array is object,String,Char if you declare it will be automatically null
at any place of array is null
Not the answer you're looking for? Browse other questions tagged c arrays null or ask your own question .
- The Overflow Blog
- How Intuit democratizes AI development across teams through reusability sponsored post
- The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie...
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
- The [amazon] tag is being burninated
- Launching the CI/CD and R Collectives and community editing features for...
- Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2
- Temporary policy: ChatGPT is banned
Hot Network Questions
- How to handle missing value if imputation doesnt make sense
- Can I tell police to wait and call a lawyer when served with a search warrant?
- How to make graphons pictures?
- Egypt - Duty free notation on passport
- Theoretically Correct vs Practical Notation
- Small bright constellation on the photo
- Should I use the mean or median of my data for queueing models?
- Is a PhD visitor considered as a visiting scholar?
- Short story taking place on a toroidal planet or moon involving flying
- Did any DOS compatibility layers exist for any UNIX-like systems before DOS started to become outmoded?
- Movie with vikings/warriors fighting an alien that looks like a wolf with tentacles
- Why does hooking voltmeter to two transformers show 0 voltage?
- What is pictured in this SHERLOC camera?
- The number of distinct words in a sentence
- Jordan's line about intimate parties in The Great Gatsby?
- The region and polygon don't match. Is it a bug?
- Why does Jesus turn to the Father to forgive in Luke 23:34?
- Experiment to crush soda can by air pressure
- If you order a special airline meal (e.g. vegan) just to try it, does this inconvenience the caterers and staff?
- Lots of pick movement
- Two Doors and a Guard
- Why are trials on "Law & Order" in the New York Supreme Court?
- Biodiversity through radiation
- Do you really get double penalized for rolling HSA excess contributions to the next year?
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
// Tutorial //
Initialize an array in c.
- C Programming

By Vijaykrishna Ram

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
In this article, we’ll take a look at how we will initialize an array in C.
There are different ways through which we can do this, so we’ll list them all one by one. Let’s get started!
Method 1: Initialize an array using an Initializer List
An initializer list initializes elements of an array in the order of the list.
For example, consider the below snippet:
This initializes an array of size 5, with the elements {1, 2, 3, 4, 5} in order.
This means that arr[0] = 1 , arr[1] = 2 , and so on.
We don’t need to initialize all the elements 0 to 4. We can even do only from indices 0 to 2.
The following code is also valid:
But now, arr[4] and arr[5] will still remain garbage values, so you need to be careful!
If you’re using an initializer list with all elements, you don’t need to mention the size of the array.
If you want to initialize all the elements to 0, there is a shortcut for this (Only for 0). We can simply mention the index as 0.
If you’re using multi-dimensional arrays, you can still initialize them all in one block, since arrays are stored in a row-wise manner.
A similar method can also be used for other datatypes, like float , char , char* , etc.
Remember, this method with [1 … 7] = “Journaldev” might not work with all compilers. I work on GCC in Linux.
Method 2: Initialize an array in C using a for loop
We can also use the for loop to set the elements of an array.
Method 3: Using Designated Initializers (For gcc compiler only)
If you’re using gcc as your C compiler, you can use designated initializers, to set a specific range of the array to the same value.
Note that there is a space between the numbers and there are the three dots. Otherwise, the compiler may think that it is a decimal point and throw an error.
Output (for gcc only)
We can also combine this with our old initializer list elements!
For example, I am setting only array index arr[0], arr[8] as 0, while the others are designated initialized to 10!
In this article, we learned how we could initialize a C array, using different methods.
For similar articles, do go through our tutorial section on C programming!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Learn more about us
Still looking for an answer?


Popular Topics
- Linux Basics
- All tutorials
- Free Managed Hosting
Try DigitalOcean for free
Join the tech talk.
Please complete your information!
- Windows Programming
- UNIX/Linux Programming
- General C++ Programming
- Set an entire array to NULL
Set an entire array to NULL

How can I assign NULL to the array pointer?

When I tried c = twoSum(ptr, 5, 345); , it's supposed to generate the following message: "There is not a tuple meeting the target". However, I could not get this message out. What's the heck here?

Since you already access c twice before checking it for NULL , compilers are allowed to optimize the check away.
Thank you for your kindly comments. Will I make changes to c if access c twice before checking it for NULL?
Move your two printf statements after the null check (the if statement).
Because you don't check for null until after you have already used "c" (in the printf statements) the compiler is allowed to optimize away the null check. If you move the printf statements after the check, the compiler won't be allowed to optimize it out.
As a rule, you should ALWAYS check that something isn't null before you use it for anything (even printf). Doing it the other way around, like you have here, is a crash waiting to happen.
Will I make changes to c if accessing c twice before checking it for NULL?
will the pointer c not be NULL after accessing it twice?
Didn't you get a crash from accessing c[0] ? Is your IDE hiding those kind of errors? Things worth investigating.
It give me 2 random numbers.
Try flushing stdout since you're not putting a newline at the end of the string
This is not working! What do you really mean?
About Community
Ranked by Size
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Arrays (C# Programming Guide)
- 3 minutes to read
- 15 contributors
You can store multiple variables of the same type in an array data structure. You declare an array by specifying the type of its elements. If you want the array to store elements of any type, you can specify object as its type. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object .
The following example creates single-dimensional, multidimensional, and jagged arrays:
Array overview
An array has the following properties:
- An array can be single-dimensional , multidimensional or jagged .
- The number of dimensions and the length of each dimension are established when the array instance is created. These values can't be changed during the lifetime of the instance.
- The default values of numeric array elements are set to zero, and reference elements are set to null .
- A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null .
- Arrays are zero indexed: an array with n elements is indexed from 0 to n-1 .
- Array elements can be of any type, including an array type.
- Array types are reference types derived from the abstract base type Array . All arrays implement IList , and IEnumerable . You can use the foreach statement to iterate through an array. Single-dimensional arrays also implement IList<T> and IEnumerable<T> .
Default value behaviour
- For value types, the array elements are initialized with the default value , the 0-bit pattern; the elements will have the value 0 .
- All the reference types (including the non-nullable ), have the values null .
- For nullable value types, HasValue is set to false and the elements would be set to null .
Arrays as Objects
In C#, arrays are actually objects, and not just addressable regions of contiguous memory as in C and C++. Array is the abstract base type of all array types. You can use the properties and other class members that Array has. An example of this is using the Length property to get the length of an array. The following code assigns the length of the numbers array, which is 5 , to a variable called lengthOfNumbers :
The Array class provides many other useful methods and properties for sorting, searching, and copying arrays. The following example uses the Rank property to display the number of dimensions of an array.
- How to use single-dimensional arrays
- How to use multi-dimensional arrays
- How to use jagged arrays
- Using foreach with arrays
- Passing arrays as arguments
- Implicitly typed arrays
- C# Programming Guide
- Collections
For more information, see the C# Language Specification . The language specification is the definitive source for C# syntax and usage.
Submit and view feedback for
Additional resources

IMAGES
VIDEO
COMMENTS
If you're talking about an array of pointers (say char ** ), you'd just say array [element] = NULL;. But it sounds as though you really want to just truncate a string ( char * ), in which case you'd actually want to write string [index] = '\0', where \0 is the null byte.
To initialise an array of char* to all NULL s: char* array [10] = { NULL }; /* The remaining elements are implicitly NULL. */ If you want to provide initial elements for execv (): char* array [10] = { "/usr/bin/ls", "-l" }; /* Again, remaining elements NULL. */ or if you wish to omit the dimension from the array declaration:
Answer (1 of 7): You can’t: you can only make a pointer null. An array variable represents a contiguous block of memory containing values of a type that doesn’t need to be a pointer type.
Method 2: Initialize an array in C using a for loop. We can also use the for loop to set the elements of an array. # include <stdio.h> int main () ...
So if Blocks [a] [b] [c] is != NULL then you must delete and then assign NULL or 0 to it. Just assigning 0 to a valid pointer will not reclaim the memory allocated but merely lose the access to that memory! Dec 12, 2013 at 2:09am Catfish666 (666) delete is an operation that frees the memory allocated by new.
Because you don't check for null until after you have already used "c" (in the printf statements) the compiler is allowed to optimize away the null check. If you move the printf statements after the check, the compiler won't be allowed to optimize it out.
As a field, an array is by default initialized to null. When using local variable arrays, we must specify this explicitly. Null fields. The C# language initializes array reference elements to null when created with the new keyword. Arrays that are fields are automatically set to null. Null Nullable new First example.
The default values of numeric array elements are set to zero, and reference elements are set to null. A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null. Arrays are zero indexed: an array with n elements is indexed from 0 to n-1.