Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I want to convert this line to C. This line is basically assigning each element in the array a value. Can anybody help me? Thank you so much

C++
char *arr = new char[10] {'A', 'K', 'U', 'E', 'B', 'Z', 'D', 'O', 'M', 'Q'};


What I have tried:

I wanted to do this but it is not working.

Objective-C
char *arr=(char *)malloc(11*sizeof(char)){'A', 'K', 'U', 'E', 'B', 'Z', 'D', 'O', 'M', 'Q'};
Posted
Updated 23-Sep-17 20:07pm

Solution 1 is correct, and will work: however do remember that that code will generate a local variable: the array of chars will be on the stack, not the heap: it will only exist until you return from the current function, and cannot be returned to the calling function in any way without causing a "hanging reference". If the array is needed outside the current function, you must use malloc and copy that array into the heap based area it returns.
 
Share this answer
 
C++
char arr[] = {'A', 'K', 'U', 'E', 'B', 'Z', 'D', 'O', 'M', 'Q'};


That is all you need to do.
You can even test the code using sizeof(arr) and you will get 10
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900