C: how to create an empty STRING?












0















Please I have been struggling for 5 hours on this, I know how to create a empty char it's like this :



char c = '';


but how can I create en empty STRING ? (because I want to use it as a temporary place to store other strings)



char temporary = "";
error : array type 'char [1]' is not assignable









share|improve this question




















  • 2





    Your question is unclear. What do you mean by an "empty char"? In any event, it is not possible in C to magically create something that can be used to contain an arbitrary and changeable string of arbitrary length, unless you dynamically allocate and manage memory. Also, the second example (in the form you've shown it) is valid - if you're getting an error from that, your code differs in some way. Consider providing an actual sample of actual code in the form of a Minimal, Complete, and Verifiable example.

    – Peter
    Nov 20 '18 at 18:29













  • tempo = ""; Assuming tempo is a char array, you can't do that. Try tempo[0] = ''. You could also do: strcpy(temp, ""); Or to completely clear it: memset(tempo, 0, sizeof(tempo);. But that's probably not necessary.

    – Johnny Mopp
    Nov 20 '18 at 18:32













  • can I do strcpy(temp, ""); without having said what temp is ???

    – masterCUCK9
    Nov 20 '18 at 18:38











  • I can't use strcpy if temp is not DEFINED

    – masterCUCK9
    Nov 20 '18 at 18:39











  • @masterCUCK9 A typo. I meant to type tempo. As in: strcpy(tempo, "");

    – Johnny Mopp
    Nov 20 '18 at 18:39


















0















Please I have been struggling for 5 hours on this, I know how to create a empty char it's like this :



char c = '';


but how can I create en empty STRING ? (because I want to use it as a temporary place to store other strings)



char temporary = "";
error : array type 'char [1]' is not assignable









share|improve this question




















  • 2





    Your question is unclear. What do you mean by an "empty char"? In any event, it is not possible in C to magically create something that can be used to contain an arbitrary and changeable string of arbitrary length, unless you dynamically allocate and manage memory. Also, the second example (in the form you've shown it) is valid - if you're getting an error from that, your code differs in some way. Consider providing an actual sample of actual code in the form of a Minimal, Complete, and Verifiable example.

    – Peter
    Nov 20 '18 at 18:29













  • tempo = ""; Assuming tempo is a char array, you can't do that. Try tempo[0] = ''. You could also do: strcpy(temp, ""); Or to completely clear it: memset(tempo, 0, sizeof(tempo);. But that's probably not necessary.

    – Johnny Mopp
    Nov 20 '18 at 18:32













  • can I do strcpy(temp, ""); without having said what temp is ???

    – masterCUCK9
    Nov 20 '18 at 18:38











  • I can't use strcpy if temp is not DEFINED

    – masterCUCK9
    Nov 20 '18 at 18:39











  • @masterCUCK9 A typo. I meant to type tempo. As in: strcpy(tempo, "");

    – Johnny Mopp
    Nov 20 '18 at 18:39
















0












0








0


1






Please I have been struggling for 5 hours on this, I know how to create a empty char it's like this :



char c = '';


but how can I create en empty STRING ? (because I want to use it as a temporary place to store other strings)



char temporary = "";
error : array type 'char [1]' is not assignable









share|improve this question
















Please I have been struggling for 5 hours on this, I know how to create a empty char it's like this :



char c = '';


but how can I create en empty STRING ? (because I want to use it as a temporary place to store other strings)



char temporary = "";
error : array type 'char [1]' is not assignable






c arrays string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 18:49







masterCUCK9

















asked Nov 20 '18 at 18:18









masterCUCK9masterCUCK9

82




82








  • 2





    Your question is unclear. What do you mean by an "empty char"? In any event, it is not possible in C to magically create something that can be used to contain an arbitrary and changeable string of arbitrary length, unless you dynamically allocate and manage memory. Also, the second example (in the form you've shown it) is valid - if you're getting an error from that, your code differs in some way. Consider providing an actual sample of actual code in the form of a Minimal, Complete, and Verifiable example.

    – Peter
    Nov 20 '18 at 18:29













  • tempo = ""; Assuming tempo is a char array, you can't do that. Try tempo[0] = ''. You could also do: strcpy(temp, ""); Or to completely clear it: memset(tempo, 0, sizeof(tempo);. But that's probably not necessary.

    – Johnny Mopp
    Nov 20 '18 at 18:32













  • can I do strcpy(temp, ""); without having said what temp is ???

    – masterCUCK9
    Nov 20 '18 at 18:38











  • I can't use strcpy if temp is not DEFINED

    – masterCUCK9
    Nov 20 '18 at 18:39











  • @masterCUCK9 A typo. I meant to type tempo. As in: strcpy(tempo, "");

    – Johnny Mopp
    Nov 20 '18 at 18:39
















  • 2





    Your question is unclear. What do you mean by an "empty char"? In any event, it is not possible in C to magically create something that can be used to contain an arbitrary and changeable string of arbitrary length, unless you dynamically allocate and manage memory. Also, the second example (in the form you've shown it) is valid - if you're getting an error from that, your code differs in some way. Consider providing an actual sample of actual code in the form of a Minimal, Complete, and Verifiable example.

    – Peter
    Nov 20 '18 at 18:29













  • tempo = ""; Assuming tempo is a char array, you can't do that. Try tempo[0] = ''. You could also do: strcpy(temp, ""); Or to completely clear it: memset(tempo, 0, sizeof(tempo);. But that's probably not necessary.

    – Johnny Mopp
    Nov 20 '18 at 18:32













  • can I do strcpy(temp, ""); without having said what temp is ???

    – masterCUCK9
    Nov 20 '18 at 18:38











  • I can't use strcpy if temp is not DEFINED

    – masterCUCK9
    Nov 20 '18 at 18:39











  • @masterCUCK9 A typo. I meant to type tempo. As in: strcpy(tempo, "");

    – Johnny Mopp
    Nov 20 '18 at 18:39










2




2





Your question is unclear. What do you mean by an "empty char"? In any event, it is not possible in C to magically create something that can be used to contain an arbitrary and changeable string of arbitrary length, unless you dynamically allocate and manage memory. Also, the second example (in the form you've shown it) is valid - if you're getting an error from that, your code differs in some way. Consider providing an actual sample of actual code in the form of a Minimal, Complete, and Verifiable example.

– Peter
Nov 20 '18 at 18:29







Your question is unclear. What do you mean by an "empty char"? In any event, it is not possible in C to magically create something that can be used to contain an arbitrary and changeable string of arbitrary length, unless you dynamically allocate and manage memory. Also, the second example (in the form you've shown it) is valid - if you're getting an error from that, your code differs in some way. Consider providing an actual sample of actual code in the form of a Minimal, Complete, and Verifiable example.

– Peter
Nov 20 '18 at 18:29















tempo = ""; Assuming tempo is a char array, you can't do that. Try tempo[0] = ''. You could also do: strcpy(temp, ""); Or to completely clear it: memset(tempo, 0, sizeof(tempo);. But that's probably not necessary.

– Johnny Mopp
Nov 20 '18 at 18:32







tempo = ""; Assuming tempo is a char array, you can't do that. Try tempo[0] = ''. You could also do: strcpy(temp, ""); Or to completely clear it: memset(tempo, 0, sizeof(tempo);. But that's probably not necessary.

– Johnny Mopp
Nov 20 '18 at 18:32















can I do strcpy(temp, ""); without having said what temp is ???

– masterCUCK9
Nov 20 '18 at 18:38





can I do strcpy(temp, ""); without having said what temp is ???

– masterCUCK9
Nov 20 '18 at 18:38













I can't use strcpy if temp is not DEFINED

– masterCUCK9
Nov 20 '18 at 18:39





I can't use strcpy if temp is not DEFINED

– masterCUCK9
Nov 20 '18 at 18:39













@masterCUCK9 A typo. I meant to type tempo. As in: strcpy(tempo, "");

– Johnny Mopp
Nov 20 '18 at 18:39







@masterCUCK9 A typo. I meant to type tempo. As in: strcpy(tempo, "");

– Johnny Mopp
Nov 20 '18 at 18:39














2 Answers
2






active

oldest

votes


















5














Define a buffer to hold the string contents:



#define BUFFER_SIZE 256 // or whatever size you need

char buffer[BUFFER_SIZE+1] = {0}; // +1 for string terminator,
// = {0} initializer zeroes out entire buffer


To assign a string to this buffer, use strcpy:



strcpy( buffer, "some string" );


To append a string to this buffer, use strcat:



strcat( buffer, "more string" );


EDIT



Now that you've edited your question, the problem is the line



tempo = "";


An array expression like tempo may not be the target of the = operator; you must use a library function like strcpy to assign string values. If you want to set tempo to an empty string, you can do any one of the following:



strcpy( tempo, "" );


or



tempo[0] = 0;


or



tempo[0] = '';





share|improve this answer


























  • I would actually memset tempo to its size as 0's to the entire buffer is reset instead of just the first character.

    – ritlew
    Nov 20 '18 at 18:38








  • 1





    @ritlew: It kind of depends on what the OP is wanting to do - if all he needs to do is make sure tempo contains an empty string before a strcpy operation, then setting the initial element to 0 is sufficient. If he's writing this buffer to a file or something, then yes, it would be safer to zero out all the unused elements.

    – John Bode
    Nov 20 '18 at 18:39













  • Well if the string is "Hello", then it becomes "ello". So if they were to strncpy 2 characters "Hi" into the buffer, it becomes "Hillo".

    – ritlew
    Nov 20 '18 at 18:42













  • AAAAYYYAAA I FOUND OUT THE PROBLEM

    – masterCUCK9
    Nov 20 '18 at 18:48











  • @ritlew: And this is one of the reasons I never use strncpy.

    – John Bode
    Nov 20 '18 at 20:11



















1














In C you have to create a temporary buffer all the time. The most typical way to do this is to write a line of code like this:



char temp_buffer[256];


This buffer will have 256 characters of space available to be able to put strings in.






share|improve this answer


























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 21 '18 at 2:15











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%2f53399156%2fc-how-to-create-an-empty-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














Define a buffer to hold the string contents:



#define BUFFER_SIZE 256 // or whatever size you need

char buffer[BUFFER_SIZE+1] = {0}; // +1 for string terminator,
// = {0} initializer zeroes out entire buffer


To assign a string to this buffer, use strcpy:



strcpy( buffer, "some string" );


To append a string to this buffer, use strcat:



strcat( buffer, "more string" );


EDIT



Now that you've edited your question, the problem is the line



tempo = "";


An array expression like tempo may not be the target of the = operator; you must use a library function like strcpy to assign string values. If you want to set tempo to an empty string, you can do any one of the following:



strcpy( tempo, "" );


or



tempo[0] = 0;


or



tempo[0] = '';





share|improve this answer


























  • I would actually memset tempo to its size as 0's to the entire buffer is reset instead of just the first character.

    – ritlew
    Nov 20 '18 at 18:38








  • 1





    @ritlew: It kind of depends on what the OP is wanting to do - if all he needs to do is make sure tempo contains an empty string before a strcpy operation, then setting the initial element to 0 is sufficient. If he's writing this buffer to a file or something, then yes, it would be safer to zero out all the unused elements.

    – John Bode
    Nov 20 '18 at 18:39













  • Well if the string is "Hello", then it becomes "ello". So if they were to strncpy 2 characters "Hi" into the buffer, it becomes "Hillo".

    – ritlew
    Nov 20 '18 at 18:42













  • AAAAYYYAAA I FOUND OUT THE PROBLEM

    – masterCUCK9
    Nov 20 '18 at 18:48











  • @ritlew: And this is one of the reasons I never use strncpy.

    – John Bode
    Nov 20 '18 at 20:11
















5














Define a buffer to hold the string contents:



#define BUFFER_SIZE 256 // or whatever size you need

char buffer[BUFFER_SIZE+1] = {0}; // +1 for string terminator,
// = {0} initializer zeroes out entire buffer


To assign a string to this buffer, use strcpy:



strcpy( buffer, "some string" );


To append a string to this buffer, use strcat:



strcat( buffer, "more string" );


EDIT



Now that you've edited your question, the problem is the line



tempo = "";


An array expression like tempo may not be the target of the = operator; you must use a library function like strcpy to assign string values. If you want to set tempo to an empty string, you can do any one of the following:



strcpy( tempo, "" );


or



tempo[0] = 0;


or



tempo[0] = '';





share|improve this answer


























  • I would actually memset tempo to its size as 0's to the entire buffer is reset instead of just the first character.

    – ritlew
    Nov 20 '18 at 18:38








  • 1





    @ritlew: It kind of depends on what the OP is wanting to do - if all he needs to do is make sure tempo contains an empty string before a strcpy operation, then setting the initial element to 0 is sufficient. If he's writing this buffer to a file or something, then yes, it would be safer to zero out all the unused elements.

    – John Bode
    Nov 20 '18 at 18:39













  • Well if the string is "Hello", then it becomes "ello". So if they were to strncpy 2 characters "Hi" into the buffer, it becomes "Hillo".

    – ritlew
    Nov 20 '18 at 18:42













  • AAAAYYYAAA I FOUND OUT THE PROBLEM

    – masterCUCK9
    Nov 20 '18 at 18:48











  • @ritlew: And this is one of the reasons I never use strncpy.

    – John Bode
    Nov 20 '18 at 20:11














5












5








5







Define a buffer to hold the string contents:



#define BUFFER_SIZE 256 // or whatever size you need

char buffer[BUFFER_SIZE+1] = {0}; // +1 for string terminator,
// = {0} initializer zeroes out entire buffer


To assign a string to this buffer, use strcpy:



strcpy( buffer, "some string" );


To append a string to this buffer, use strcat:



strcat( buffer, "more string" );


EDIT



Now that you've edited your question, the problem is the line



tempo = "";


An array expression like tempo may not be the target of the = operator; you must use a library function like strcpy to assign string values. If you want to set tempo to an empty string, you can do any one of the following:



strcpy( tempo, "" );


or



tempo[0] = 0;


or



tempo[0] = '';





share|improve this answer















Define a buffer to hold the string contents:



#define BUFFER_SIZE 256 // or whatever size you need

char buffer[BUFFER_SIZE+1] = {0}; // +1 for string terminator,
// = {0} initializer zeroes out entire buffer


To assign a string to this buffer, use strcpy:



strcpy( buffer, "some string" );


To append a string to this buffer, use strcat:



strcat( buffer, "more string" );


EDIT



Now that you've edited your question, the problem is the line



tempo = "";


An array expression like tempo may not be the target of the = operator; you must use a library function like strcpy to assign string values. If you want to set tempo to an empty string, you can do any one of the following:



strcpy( tempo, "" );


or



tempo[0] = 0;


or



tempo[0] = '';






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 18:36

























answered Nov 20 '18 at 18:30









John BodeJohn Bode

83k1378150




83k1378150













  • I would actually memset tempo to its size as 0's to the entire buffer is reset instead of just the first character.

    – ritlew
    Nov 20 '18 at 18:38








  • 1





    @ritlew: It kind of depends on what the OP is wanting to do - if all he needs to do is make sure tempo contains an empty string before a strcpy operation, then setting the initial element to 0 is sufficient. If he's writing this buffer to a file or something, then yes, it would be safer to zero out all the unused elements.

    – John Bode
    Nov 20 '18 at 18:39













  • Well if the string is "Hello", then it becomes "ello". So if they were to strncpy 2 characters "Hi" into the buffer, it becomes "Hillo".

    – ritlew
    Nov 20 '18 at 18:42













  • AAAAYYYAAA I FOUND OUT THE PROBLEM

    – masterCUCK9
    Nov 20 '18 at 18:48











  • @ritlew: And this is one of the reasons I never use strncpy.

    – John Bode
    Nov 20 '18 at 20:11



















  • I would actually memset tempo to its size as 0's to the entire buffer is reset instead of just the first character.

    – ritlew
    Nov 20 '18 at 18:38








  • 1





    @ritlew: It kind of depends on what the OP is wanting to do - if all he needs to do is make sure tempo contains an empty string before a strcpy operation, then setting the initial element to 0 is sufficient. If he's writing this buffer to a file or something, then yes, it would be safer to zero out all the unused elements.

    – John Bode
    Nov 20 '18 at 18:39













  • Well if the string is "Hello", then it becomes "ello". So if they were to strncpy 2 characters "Hi" into the buffer, it becomes "Hillo".

    – ritlew
    Nov 20 '18 at 18:42













  • AAAAYYYAAA I FOUND OUT THE PROBLEM

    – masterCUCK9
    Nov 20 '18 at 18:48











  • @ritlew: And this is one of the reasons I never use strncpy.

    – John Bode
    Nov 20 '18 at 20:11

















I would actually memset tempo to its size as 0's to the entire buffer is reset instead of just the first character.

– ritlew
Nov 20 '18 at 18:38







I would actually memset tempo to its size as 0's to the entire buffer is reset instead of just the first character.

– ritlew
Nov 20 '18 at 18:38






1




1





@ritlew: It kind of depends on what the OP is wanting to do - if all he needs to do is make sure tempo contains an empty string before a strcpy operation, then setting the initial element to 0 is sufficient. If he's writing this buffer to a file or something, then yes, it would be safer to zero out all the unused elements.

– John Bode
Nov 20 '18 at 18:39







@ritlew: It kind of depends on what the OP is wanting to do - if all he needs to do is make sure tempo contains an empty string before a strcpy operation, then setting the initial element to 0 is sufficient. If he's writing this buffer to a file or something, then yes, it would be safer to zero out all the unused elements.

– John Bode
Nov 20 '18 at 18:39















Well if the string is "Hello", then it becomes "ello". So if they were to strncpy 2 characters "Hi" into the buffer, it becomes "Hillo".

– ritlew
Nov 20 '18 at 18:42







Well if the string is "Hello", then it becomes "ello". So if they were to strncpy 2 characters "Hi" into the buffer, it becomes "Hillo".

– ritlew
Nov 20 '18 at 18:42















AAAAYYYAAA I FOUND OUT THE PROBLEM

– masterCUCK9
Nov 20 '18 at 18:48





AAAAYYYAAA I FOUND OUT THE PROBLEM

– masterCUCK9
Nov 20 '18 at 18:48













@ritlew: And this is one of the reasons I never use strncpy.

– John Bode
Nov 20 '18 at 20:11





@ritlew: And this is one of the reasons I never use strncpy.

– John Bode
Nov 20 '18 at 20:11













1














In C you have to create a temporary buffer all the time. The most typical way to do this is to write a line of code like this:



char temp_buffer[256];


This buffer will have 256 characters of space available to be able to put strings in.






share|improve this answer


























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 21 '18 at 2:15
















1














In C you have to create a temporary buffer all the time. The most typical way to do this is to write a line of code like this:



char temp_buffer[256];


This buffer will have 256 characters of space available to be able to put strings in.






share|improve this answer


























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 21 '18 at 2:15














1












1








1







In C you have to create a temporary buffer all the time. The most typical way to do this is to write a line of code like this:



char temp_buffer[256];


This buffer will have 256 characters of space available to be able to put strings in.






share|improve this answer















In C you have to create a temporary buffer all the time. The most typical way to do this is to write a line of code like this:



char temp_buffer[256];


This buffer will have 256 characters of space available to be able to put strings in.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 18:50

























answered Nov 20 '18 at 18:22









ritlewritlew

853411




853411













  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 21 '18 at 2:15



















  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 21 '18 at 2:15

















Comments are not for extended discussion; this conversation has been moved to chat.

– Samuel Liew
Nov 21 '18 at 2:15





Comments are not for extended discussion; this conversation has been moved to chat.

– Samuel Liew
Nov 21 '18 at 2:15


















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%2f53399156%2fc-how-to-create-an-empty-string%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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)