• 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

Nazerke's user avatar

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:

hmjd's user avatar

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

Omkant's user avatar

execv takes an array of char * , not an array of char .

user93353's user avatar

IF your array is object,String,Char if you declare it will be automatically null

at any place of array is null

Oldemiro Henry Williams Baloi's user avatar

Not the answer you're looking for? Browse other questions tagged c arrays null or ask your own question .

Hot Network Questions

c assign null to array

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.

Default avatar

By Vijaykrishna Ram

Initialize an Array in C

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?

Creative Commons

Popular Topics

Try DigitalOcean for free

Join the tech talk.

Please complete your information!

  Set an entire array to NULL

c assign null to array

How can I assign NULL to the array pointer?

c assign null to array

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?

' src=

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)

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:

Default value behaviour

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.

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

  1. c#

    c assign null to array

  2. C Program To Access An Element In 2d Array

    c assign null to array

  3. C++ Lesson 13.0

    c assign null to array

  4. Array Initialization in C Programming

    c assign null to array

  5. C++ Array Lab Exercise 1

    c assign null to array

  6. C Programming Tutorial

    c assign null to array

VIDEO

  1. How to Resize an Array in C/C++? Is there a best way?

  2. Can A JSON File With Duplicate Attributes be Loaded Into Snowflake

  3. Double Ratio and End Point on RC8X

  4. Experience the OneUI 5.0 in the new #GalaxyA14 5G and #AmpYourAwesome

  5. 05 Data Structures Introduction and Array

  6. 6. I will show you how great I am

COMMENTS

  1. How to set element in array to null in C program - Stack Overflow

    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.

  2. c - initializing array element to NULL - Stack Overflow

    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:

  3. How to make an array null in C - Quora

    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.

  4. Initialize an Array in C | DigitalOcean

    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 () ...

  5. Set an entire array to NULL - C++ Forum - cplusplus.com

    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.

  6. How can I assign NULL to the array pointer? : C_Programming

    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.

  7. C# Null Array - Dot Net Perls

    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.

  8. Arrays - C# Programming Guide | Microsoft Learn

    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.