array initializer must be an initializer list or string literal c










0















I am newbie in C. I do not know why I am getting this error and how to fix it?



void FPS_PRINT_ENROLLED()
int checkNum = 0;
int ID = 0;
int num_Enroll = 0;
num_Enroll = Available_ID();
char strNum[3] = 0;
itoa(num_Enroll, strNum);
uint32_t numLen = strlen((char *)strNum);
UART_send_A3("Number of Stored Prints: ", 25);
UART_send_A3(&strNum, numLen);



The error message is: array initializer must be an initializer list or string literal



Please see the attached screenshot of the error message. Also, the c file is attached.



Screenshot of error message



By the way, this is in PSoC Creator 4.1 which uses C language.



code file download link through GoogleDrive










share|improve this question



















  • 1





    this line: char strNum[3] = 0; is a problem. The variable on the left is an array of chars with three elements. The initializer on the right is a a scalar int. They types do not match. Try putting the initializer in curly braces which will assign 0 to each element, like this: char strNum[3] = 0;

    – bruceg
    Nov 14 '18 at 3:21






  • 1





    @bruceg Such a nice explanation. This should go in as an answer.

    – Sandeep
    Nov 14 '18 at 3:32






  • 1





    Great @bruceg , thanks!

    – Sulaiman Alsuhaimi
    Nov 14 '18 at 5:35















0















I am newbie in C. I do not know why I am getting this error and how to fix it?



void FPS_PRINT_ENROLLED()
int checkNum = 0;
int ID = 0;
int num_Enroll = 0;
num_Enroll = Available_ID();
char strNum[3] = 0;
itoa(num_Enroll, strNum);
uint32_t numLen = strlen((char *)strNum);
UART_send_A3("Number of Stored Prints: ", 25);
UART_send_A3(&strNum, numLen);



The error message is: array initializer must be an initializer list or string literal



Please see the attached screenshot of the error message. Also, the c file is attached.



Screenshot of error message



By the way, this is in PSoC Creator 4.1 which uses C language.



code file download link through GoogleDrive










share|improve this question



















  • 1





    this line: char strNum[3] = 0; is a problem. The variable on the left is an array of chars with three elements. The initializer on the right is a a scalar int. They types do not match. Try putting the initializer in curly braces which will assign 0 to each element, like this: char strNum[3] = 0;

    – bruceg
    Nov 14 '18 at 3:21






  • 1





    @bruceg Such a nice explanation. This should go in as an answer.

    – Sandeep
    Nov 14 '18 at 3:32






  • 1





    Great @bruceg , thanks!

    – Sulaiman Alsuhaimi
    Nov 14 '18 at 5:35













0












0








0


1






I am newbie in C. I do not know why I am getting this error and how to fix it?



void FPS_PRINT_ENROLLED()
int checkNum = 0;
int ID = 0;
int num_Enroll = 0;
num_Enroll = Available_ID();
char strNum[3] = 0;
itoa(num_Enroll, strNum);
uint32_t numLen = strlen((char *)strNum);
UART_send_A3("Number of Stored Prints: ", 25);
UART_send_A3(&strNum, numLen);



The error message is: array initializer must be an initializer list or string literal



Please see the attached screenshot of the error message. Also, the c file is attached.



Screenshot of error message



By the way, this is in PSoC Creator 4.1 which uses C language.



code file download link through GoogleDrive










share|improve this question
















I am newbie in C. I do not know why I am getting this error and how to fix it?



void FPS_PRINT_ENROLLED()
int checkNum = 0;
int ID = 0;
int num_Enroll = 0;
num_Enroll = Available_ID();
char strNum[3] = 0;
itoa(num_Enroll, strNum);
uint32_t numLen = strlen((char *)strNum);
UART_send_A3("Number of Stored Prints: ", 25);
UART_send_A3(&strNum, numLen);



The error message is: array initializer must be an initializer list or string literal



Please see the attached screenshot of the error message. Also, the c file is attached.



Screenshot of error message



By the way, this is in PSoC Creator 4.1 which uses C language.



code file download link through GoogleDrive







c string literals initializer psoc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 5:33









kenlukas

1,28731317




1,28731317










asked Nov 14 '18 at 3:18









Sulaiman AlsuhaimiSulaiman Alsuhaimi

31




31







  • 1





    this line: char strNum[3] = 0; is a problem. The variable on the left is an array of chars with three elements. The initializer on the right is a a scalar int. They types do not match. Try putting the initializer in curly braces which will assign 0 to each element, like this: char strNum[3] = 0;

    – bruceg
    Nov 14 '18 at 3:21






  • 1





    @bruceg Such a nice explanation. This should go in as an answer.

    – Sandeep
    Nov 14 '18 at 3:32






  • 1





    Great @bruceg , thanks!

    – Sulaiman Alsuhaimi
    Nov 14 '18 at 5:35












  • 1





    this line: char strNum[3] = 0; is a problem. The variable on the left is an array of chars with three elements. The initializer on the right is a a scalar int. They types do not match. Try putting the initializer in curly braces which will assign 0 to each element, like this: char strNum[3] = 0;

    – bruceg
    Nov 14 '18 at 3:21






  • 1





    @bruceg Such a nice explanation. This should go in as an answer.

    – Sandeep
    Nov 14 '18 at 3:32






  • 1





    Great @bruceg , thanks!

    – Sulaiman Alsuhaimi
    Nov 14 '18 at 5:35







1




1





this line: char strNum[3] = 0; is a problem. The variable on the left is an array of chars with three elements. The initializer on the right is a a scalar int. They types do not match. Try putting the initializer in curly braces which will assign 0 to each element, like this: char strNum[3] = 0;

– bruceg
Nov 14 '18 at 3:21





this line: char strNum[3] = 0; is a problem. The variable on the left is an array of chars with three elements. The initializer on the right is a a scalar int. They types do not match. Try putting the initializer in curly braces which will assign 0 to each element, like this: char strNum[3] = 0;

– bruceg
Nov 14 '18 at 3:21




1




1





@bruceg Such a nice explanation. This should go in as an answer.

– Sandeep
Nov 14 '18 at 3:32





@bruceg Such a nice explanation. This should go in as an answer.

– Sandeep
Nov 14 '18 at 3:32




1




1





Great @bruceg , thanks!

– Sulaiman Alsuhaimi
Nov 14 '18 at 5:35





Great @bruceg , thanks!

– Sulaiman Alsuhaimi
Nov 14 '18 at 5:35












1 Answer
1






active

oldest

votes


















0














char strNum[3] = 0;


This line creates a three character string. You then use it as a string with the function strlen and the uart_send.



As a C string it must be terminated with a null character, so you only have two usable characters. It must also be initialised as a string, the compiler is telling you that you haven't done this correctly.



Try something like one of these lines



char strNum[3] = ""; // Empty string
char strNum[3] = "AB"; // Full string
char strNum[3] = "0"; // String holding the character zero





share|improve this answer


















  • 1





    Detail: Although all 3 array elements are initialized to zero, the string is only 1 character: strNum[0].

    – chux
    Nov 14 '18 at 3:43












  • Awesome! I changed it to char strNum[3] = "0"; and got it working. Will this affect my code if I keep it like this or you are just explaining to me?

    – Sulaiman Alsuhaimi
    Nov 14 '18 at 5:30












  • No, it is a perfectly valid piece of code. Keep in mind that it is a string representation of a number, not a number. You can't increment it or modify it like you would a number. If you have issues with that it would be best to ask another question.

    – lod
    Nov 14 '18 at 5:43











  • If your statement “It must also be initialised as a string” is meant to say that an array of char must be initialized with a string literal, then it is incorrect. As the compiler message notes, it may be initialized with an initializer list (enclosed in braces), such as 2, 1, 0 or 'a', 'b', 0 .

    – Eric Postpischil
    Nov 14 '18 at 11:59











  • char strNum[3] = 0; doesn't create jack, it is not valid C. Hence the compiler error.

    – Lundin
    Nov 14 '18 at 12:20










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292705%2farray-initializer-must-be-an-initializer-list-or-string-literal-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














char strNum[3] = 0;


This line creates a three character string. You then use it as a string with the function strlen and the uart_send.



As a C string it must be terminated with a null character, so you only have two usable characters. It must also be initialised as a string, the compiler is telling you that you haven't done this correctly.



Try something like one of these lines



char strNum[3] = ""; // Empty string
char strNum[3] = "AB"; // Full string
char strNum[3] = "0"; // String holding the character zero





share|improve this answer


















  • 1





    Detail: Although all 3 array elements are initialized to zero, the string is only 1 character: strNum[0].

    – chux
    Nov 14 '18 at 3:43












  • Awesome! I changed it to char strNum[3] = "0"; and got it working. Will this affect my code if I keep it like this or you are just explaining to me?

    – Sulaiman Alsuhaimi
    Nov 14 '18 at 5:30












  • No, it is a perfectly valid piece of code. Keep in mind that it is a string representation of a number, not a number. You can't increment it or modify it like you would a number. If you have issues with that it would be best to ask another question.

    – lod
    Nov 14 '18 at 5:43











  • If your statement “It must also be initialised as a string” is meant to say that an array of char must be initialized with a string literal, then it is incorrect. As the compiler message notes, it may be initialized with an initializer list (enclosed in braces), such as 2, 1, 0 or 'a', 'b', 0 .

    – Eric Postpischil
    Nov 14 '18 at 11:59











  • char strNum[3] = 0; doesn't create jack, it is not valid C. Hence the compiler error.

    – Lundin
    Nov 14 '18 at 12:20















0














char strNum[3] = 0;


This line creates a three character string. You then use it as a string with the function strlen and the uart_send.



As a C string it must be terminated with a null character, so you only have two usable characters. It must also be initialised as a string, the compiler is telling you that you haven't done this correctly.



Try something like one of these lines



char strNum[3] = ""; // Empty string
char strNum[3] = "AB"; // Full string
char strNum[3] = "0"; // String holding the character zero





share|improve this answer


















  • 1





    Detail: Although all 3 array elements are initialized to zero, the string is only 1 character: strNum[0].

    – chux
    Nov 14 '18 at 3:43












  • Awesome! I changed it to char strNum[3] = "0"; and got it working. Will this affect my code if I keep it like this or you are just explaining to me?

    – Sulaiman Alsuhaimi
    Nov 14 '18 at 5:30












  • No, it is a perfectly valid piece of code. Keep in mind that it is a string representation of a number, not a number. You can't increment it or modify it like you would a number. If you have issues with that it would be best to ask another question.

    – lod
    Nov 14 '18 at 5:43











  • If your statement “It must also be initialised as a string” is meant to say that an array of char must be initialized with a string literal, then it is incorrect. As the compiler message notes, it may be initialized with an initializer list (enclosed in braces), such as 2, 1, 0 or 'a', 'b', 0 .

    – Eric Postpischil
    Nov 14 '18 at 11:59











  • char strNum[3] = 0; doesn't create jack, it is not valid C. Hence the compiler error.

    – Lundin
    Nov 14 '18 at 12:20













0












0








0







char strNum[3] = 0;


This line creates a three character string. You then use it as a string with the function strlen and the uart_send.



As a C string it must be terminated with a null character, so you only have two usable characters. It must also be initialised as a string, the compiler is telling you that you haven't done this correctly.



Try something like one of these lines



char strNum[3] = ""; // Empty string
char strNum[3] = "AB"; // Full string
char strNum[3] = "0"; // String holding the character zero





share|improve this answer













char strNum[3] = 0;


This line creates a three character string. You then use it as a string with the function strlen and the uart_send.



As a C string it must be terminated with a null character, so you only have two usable characters. It must also be initialised as a string, the compiler is telling you that you haven't done this correctly.



Try something like one of these lines



char strNum[3] = ""; // Empty string
char strNum[3] = "AB"; // Full string
char strNum[3] = "0"; // String holding the character zero






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 3:32









lodlod

953612




953612







  • 1





    Detail: Although all 3 array elements are initialized to zero, the string is only 1 character: strNum[0].

    – chux
    Nov 14 '18 at 3:43












  • Awesome! I changed it to char strNum[3] = "0"; and got it working. Will this affect my code if I keep it like this or you are just explaining to me?

    – Sulaiman Alsuhaimi
    Nov 14 '18 at 5:30












  • No, it is a perfectly valid piece of code. Keep in mind that it is a string representation of a number, not a number. You can't increment it or modify it like you would a number. If you have issues with that it would be best to ask another question.

    – lod
    Nov 14 '18 at 5:43











  • If your statement “It must also be initialised as a string” is meant to say that an array of char must be initialized with a string literal, then it is incorrect. As the compiler message notes, it may be initialized with an initializer list (enclosed in braces), such as 2, 1, 0 or 'a', 'b', 0 .

    – Eric Postpischil
    Nov 14 '18 at 11:59











  • char strNum[3] = 0; doesn't create jack, it is not valid C. Hence the compiler error.

    – Lundin
    Nov 14 '18 at 12:20












  • 1





    Detail: Although all 3 array elements are initialized to zero, the string is only 1 character: strNum[0].

    – chux
    Nov 14 '18 at 3:43












  • Awesome! I changed it to char strNum[3] = "0"; and got it working. Will this affect my code if I keep it like this or you are just explaining to me?

    – Sulaiman Alsuhaimi
    Nov 14 '18 at 5:30












  • No, it is a perfectly valid piece of code. Keep in mind that it is a string representation of a number, not a number. You can't increment it or modify it like you would a number. If you have issues with that it would be best to ask another question.

    – lod
    Nov 14 '18 at 5:43











  • If your statement “It must also be initialised as a string” is meant to say that an array of char must be initialized with a string literal, then it is incorrect. As the compiler message notes, it may be initialized with an initializer list (enclosed in braces), such as 2, 1, 0 or 'a', 'b', 0 .

    – Eric Postpischil
    Nov 14 '18 at 11:59











  • char strNum[3] = 0; doesn't create jack, it is not valid C. Hence the compiler error.

    – Lundin
    Nov 14 '18 at 12:20







1




1





Detail: Although all 3 array elements are initialized to zero, the string is only 1 character: strNum[0].

– chux
Nov 14 '18 at 3:43






Detail: Although all 3 array elements are initialized to zero, the string is only 1 character: strNum[0].

– chux
Nov 14 '18 at 3:43














Awesome! I changed it to char strNum[3] = "0"; and got it working. Will this affect my code if I keep it like this or you are just explaining to me?

– Sulaiman Alsuhaimi
Nov 14 '18 at 5:30






Awesome! I changed it to char strNum[3] = "0"; and got it working. Will this affect my code if I keep it like this or you are just explaining to me?

– Sulaiman Alsuhaimi
Nov 14 '18 at 5:30














No, it is a perfectly valid piece of code. Keep in mind that it is a string representation of a number, not a number. You can't increment it or modify it like you would a number. If you have issues with that it would be best to ask another question.

– lod
Nov 14 '18 at 5:43





No, it is a perfectly valid piece of code. Keep in mind that it is a string representation of a number, not a number. You can't increment it or modify it like you would a number. If you have issues with that it would be best to ask another question.

– lod
Nov 14 '18 at 5:43













If your statement “It must also be initialised as a string” is meant to say that an array of char must be initialized with a string literal, then it is incorrect. As the compiler message notes, it may be initialized with an initializer list (enclosed in braces), such as 2, 1, 0 or 'a', 'b', 0 .

– Eric Postpischil
Nov 14 '18 at 11:59





If your statement “It must also be initialised as a string” is meant to say that an array of char must be initialized with a string literal, then it is incorrect. As the compiler message notes, it may be initialized with an initializer list (enclosed in braces), such as 2, 1, 0 or 'a', 'b', 0 .

– Eric Postpischil
Nov 14 '18 at 11:59













char strNum[3] = 0; doesn't create jack, it is not valid C. Hence the compiler error.

– Lundin
Nov 14 '18 at 12:20





char strNum[3] = 0; doesn't create jack, it is not valid C. Hence the compiler error.

– Lundin
Nov 14 '18 at 12:20

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292705%2farray-initializer-must-be-an-initializer-list-or-string-literal-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto