poppy
poppy

Reputation: 153

How to store zero as array data in C

I have array in which i want to store values like this 1203

   char* arr= new char[10];
    arr[0] = 1;
    arr[1] = 2;
    arr[2] = 0;
    arr[3] = 3;

but after storing 0 i am not able to see any further data bcs it'll consider that as end of the data. is there any way to manage this??

Upvotes: 0

Views: 1564

Answers (8)

Hicham
Hicham

Reputation: 979

0 means end of string in the char*.

Because char* is a string in C and it exists to be used as char array not as integer array. All function related on string are intended to be used with a string.

You can use int* to store integers.

You can do the binary operations manually like this :

int value = 1245; 

char* temp = new char[4]; 

temp[0] = (char)(value >> 24);
temp[1] = (char)(value >> 16);
temp[2] = (char)(value >> 8); 
temp[3] = (char)value;

return temp;

you can use uint8_t or int instead of char.

the (char*)((int)value) is not good because you cast the int to char*, but the char* is the variable temp not the number to be stored in temp.

If pointers is a new thing for you, it will be helpful to do some exercises in pointers : manipulate arrays, lists,...

Upvotes: 1

N4553R
N4553R

Reputation: 188

you can use write on Unix platforms:

man 2 write;

this function allow you to specify the length of element to print.

but for sure, snprintf is a portable solution (as size_t size allow you to do it so).

doing that be careful in order not to segfault.

nb: you can set char with value like 0 or 255, or with negative value if it's a signed char... you can set it with 0, or with 48, there are no differences.

Upvotes: 0

Kinjal Patel
Kinjal Patel

Reputation: 405

char* arr= new char[10];

as you have defined array of characters, when you assign 0 to any array member and pass whole array or print the array, the reason it takes it as data end because 0 is actually NULL and every character string is terminated by NULL. So if you really want to assign value of character 0 then you can assign ascii value of 0 or '0'.

hope it helps

Upvotes: 0

Eric Postpischil
Eric Postpischil

Reputation: 222437

You say you are “not able to see any further data”, but you did not describe what you are doing to see the data. Are you printing it with printf and a %s format? Are you displaying it in a debugger?

When you use string operations on char data, a zero commonly indicates the end of the string. This is true when using %s with printf or when using strcpy or strlen. However, an array of char may be treated numerically. After arr[3] = 3;, 3 is stored in arr[3], and it is just a matter of seeing it.

You can print char data as decimal numerals by using the %d format with printf. %d prints one number, so you need to pass it one number to print, such as arr[0], arr[3], or, in a loop, arr[i]. This is different from %s, where you pass a pointer (such as the array, which becomes a pointer to the first element) to printf, and it prints multiple characters.

If you are looking at the char array with a debugger, you can likely look at arr[3] individually to see that it contains 3. Your debugger may have a way to display an array of char as a sequence of decimal numerals instead of as a string.

Upvotes: 1

jpinto3912
jpinto3912

Reputation: 1465

Your debugger tool, as most, will take notice that it is displaying a char array. That from of data is commonly used in C as storage for null-terminated strings.

A null-terminated string, if the named wasn't revealing enough, is a bunch of characters (printable or not), finished by a zero. That mark tells printf and alike when to stop printing.

It also told the function used by the debugger to stop printing. To avoid this you should either tell your debugger "this ain't no string, fool!" or simply redeclare as short.

You seam to be a starter so take this advice: forget about ints, use short or longs. The int size is machine dependent, and it will get you into "I assummed it was Xbits" trouble later on.

Upvotes: 0

jxh
jxh

Reputation: 70392

You're syntax was in C++. But, disregarding that, you could use snprintf to store your data:

snprintf(arr, 10, "%d", 1203);

Upvotes: 2

Mahmoud Fayez
Mahmoud Fayez

Reputation: 3459

I think you should write it as:

char* arr= new char[10];
arr[0] = '1';
arr[1] = '2';
arr[2] = '0';
arr[3] = '3';

Such that you do not need to use zero-value which indicates the termination as said by Aamir

Upvotes: 0

Aamir
Aamir

Reputation: 15546

You are storing integers in a character array. Try something like this:

char* arr= new char[10];
arr[0] = '1';
arr[1] = '2';
arr[2] = '0';
arr[3] = '3';

Upvotes: 2

Related Questions