c printing the actual content of string
I want to print a string, which name was contained by another string.
My code is this:
int i=1; //forexample
char end[9];
char start[25]="kerdesek[index]."; //my question(kerdes) array
if(i==1){
melyikkerdes='A';
strcpy(vege,"a_valasz"); //the exact answer(=valasz) i want to
} //print
else if(i==2){
melyikkerdes='B';
strcpy(vege,"b_valasz");
}
else if(i==3){
melyikkerdes='C';
strcpy(vege,"c_valasz");
}
else{
melyikkerdes='D';
strcpy(vege,"d_valasz");
}
strcat(end,start);
printf("%s",start"); //i want to print the value of the
//concatenated string here
My program prints:
kerdesek[index].a_valasz
Do you have any solution? .
c string
|
show 3 more comments
I want to print a string, which name was contained by another string.
My code is this:
int i=1; //forexample
char end[9];
char start[25]="kerdesek[index]."; //my question(kerdes) array
if(i==1){
melyikkerdes='A';
strcpy(vege,"a_valasz"); //the exact answer(=valasz) i want to
} //print
else if(i==2){
melyikkerdes='B';
strcpy(vege,"b_valasz");
}
else if(i==3){
melyikkerdes='C';
strcpy(vege,"c_valasz");
}
else{
melyikkerdes='D';
strcpy(vege,"d_valasz");
}
strcat(end,start);
printf("%s",start"); //i want to print the value of the
//concatenated string here
My program prints:
kerdesek[index].a_valasz
Do you have any solution? .
c string
Could you add example input and output?
– J...S
Nov 25 '18 at 5:25
from my sturct (kerdesek[index].a_valasz) i have to print "apple", but it only prints the name of the path, not the exact string, that the path contains
– Gergely Halász
Nov 25 '18 at 5:33
You are usingendwithout initializing it.
– H.S.
Nov 25 '18 at 5:35
i is hard coded to 1, then you concatenate "a_valasz" to "kerdesek[index]." giving your answer, so... it is doing what you are telling it to.
– SteveFerg
Nov 25 '18 at 5:35
sry i translated it bad. vege=end
– Gergely Halász
Nov 25 '18 at 5:36
|
show 3 more comments
I want to print a string, which name was contained by another string.
My code is this:
int i=1; //forexample
char end[9];
char start[25]="kerdesek[index]."; //my question(kerdes) array
if(i==1){
melyikkerdes='A';
strcpy(vege,"a_valasz"); //the exact answer(=valasz) i want to
} //print
else if(i==2){
melyikkerdes='B';
strcpy(vege,"b_valasz");
}
else if(i==3){
melyikkerdes='C';
strcpy(vege,"c_valasz");
}
else{
melyikkerdes='D';
strcpy(vege,"d_valasz");
}
strcat(end,start);
printf("%s",start"); //i want to print the value of the
//concatenated string here
My program prints:
kerdesek[index].a_valasz
Do you have any solution? .
c string
I want to print a string, which name was contained by another string.
My code is this:
int i=1; //forexample
char end[9];
char start[25]="kerdesek[index]."; //my question(kerdes) array
if(i==1){
melyikkerdes='A';
strcpy(vege,"a_valasz"); //the exact answer(=valasz) i want to
} //print
else if(i==2){
melyikkerdes='B';
strcpy(vege,"b_valasz");
}
else if(i==3){
melyikkerdes='C';
strcpy(vege,"c_valasz");
}
else{
melyikkerdes='D';
strcpy(vege,"d_valasz");
}
strcat(end,start);
printf("%s",start"); //i want to print the value of the
//concatenated string here
My program prints:
kerdesek[index].a_valasz
Do you have any solution? .
c string
c string
edited Nov 25 '18 at 8:16
Md. Mokammal Hossen Farnan
586320
586320
asked Nov 25 '18 at 5:22
Gergely HalászGergely Halász
1
1
Could you add example input and output?
– J...S
Nov 25 '18 at 5:25
from my sturct (kerdesek[index].a_valasz) i have to print "apple", but it only prints the name of the path, not the exact string, that the path contains
– Gergely Halász
Nov 25 '18 at 5:33
You are usingendwithout initializing it.
– H.S.
Nov 25 '18 at 5:35
i is hard coded to 1, then you concatenate "a_valasz" to "kerdesek[index]." giving your answer, so... it is doing what you are telling it to.
– SteveFerg
Nov 25 '18 at 5:35
sry i translated it bad. vege=end
– Gergely Halász
Nov 25 '18 at 5:36
|
show 3 more comments
Could you add example input and output?
– J...S
Nov 25 '18 at 5:25
from my sturct (kerdesek[index].a_valasz) i have to print "apple", but it only prints the name of the path, not the exact string, that the path contains
– Gergely Halász
Nov 25 '18 at 5:33
You are usingendwithout initializing it.
– H.S.
Nov 25 '18 at 5:35
i is hard coded to 1, then you concatenate "a_valasz" to "kerdesek[index]." giving your answer, so... it is doing what you are telling it to.
– SteveFerg
Nov 25 '18 at 5:35
sry i translated it bad. vege=end
– Gergely Halász
Nov 25 '18 at 5:36
Could you add example input and output?
– J...S
Nov 25 '18 at 5:25
Could you add example input and output?
– J...S
Nov 25 '18 at 5:25
from my sturct (kerdesek[index].a_valasz) i have to print "apple", but it only prints the name of the path, not the exact string, that the path contains
– Gergely Halász
Nov 25 '18 at 5:33
from my sturct (kerdesek[index].a_valasz) i have to print "apple", but it only prints the name of the path, not the exact string, that the path contains
– Gergely Halász
Nov 25 '18 at 5:33
You are using
end without initializing it.– H.S.
Nov 25 '18 at 5:35
You are using
end without initializing it.– H.S.
Nov 25 '18 at 5:35
i is hard coded to 1, then you concatenate "a_valasz" to "kerdesek[index]." giving your answer, so... it is doing what you are telling it to.
– SteveFerg
Nov 25 '18 at 5:35
i is hard coded to 1, then you concatenate "a_valasz" to "kerdesek[index]." giving your answer, so... it is doing what you are telling it to.
– SteveFerg
Nov 25 '18 at 5:35
sry i translated it bad. vege=end
– Gergely Halász
Nov 25 '18 at 5:36
sry i translated it bad. vege=end
– Gergely Halász
Nov 25 '18 at 5:36
|
show 3 more comments
2 Answers
2
active
oldest
votes
C has no way to evalutate the contents of a string as an expression. You may be able to do what you want with pointers, however:
int i=1; //forexample
struct kerdesek_type *kelem = &kerdesek[index];
char *string = 0;
if(i==1){
melyikkerdes='A';
string = kelem->a_valasz;
}
else if(i==2){
melyikkerdes='B';
string = kelem->b_valasz;
}
else if(i==3){
melyikkerdes='C';
string = kelem->c_valasz;
}
else{
melyikkerdes='D';
string = kelem->d_valasz;
}
printf("%s",string);
add a comment |
If I understand you correctly, you don't want to print
kerdesek[index].a_valasz
but rather what is contained in the structure element?
You cannot construct C code as strings at runtime and have them an executable part of your program.
Is this what you are trying to do?
#include <stdio.h>
#define MAX_RECORDS 2
struct ker
{
char a_answer[50];
char b_answer[50];
};
int main()
{
struct ker questions[MAX_RECORDS] = {{"a_one", "b_one"},{"a_two", "b_two"}};
char *reply_string = "None";
int answer_number = 2; //Example
int question_number = 1; //Example
switch (answer_number){
case 1:
reply_string = questions[question_number].a_answer;
break;
case 2:
reply_string = questions[question_number].b_answer;
break;
default:
reply_string = "None";
break;
}
printf("%sn", reply_string);
return 0;
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464884%2fc-printing-the-actual-content-of-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
C has no way to evalutate the contents of a string as an expression. You may be able to do what you want with pointers, however:
int i=1; //forexample
struct kerdesek_type *kelem = &kerdesek[index];
char *string = 0;
if(i==1){
melyikkerdes='A';
string = kelem->a_valasz;
}
else if(i==2){
melyikkerdes='B';
string = kelem->b_valasz;
}
else if(i==3){
melyikkerdes='C';
string = kelem->c_valasz;
}
else{
melyikkerdes='D';
string = kelem->d_valasz;
}
printf("%s",string);
add a comment |
C has no way to evalutate the contents of a string as an expression. You may be able to do what you want with pointers, however:
int i=1; //forexample
struct kerdesek_type *kelem = &kerdesek[index];
char *string = 0;
if(i==1){
melyikkerdes='A';
string = kelem->a_valasz;
}
else if(i==2){
melyikkerdes='B';
string = kelem->b_valasz;
}
else if(i==3){
melyikkerdes='C';
string = kelem->c_valasz;
}
else{
melyikkerdes='D';
string = kelem->d_valasz;
}
printf("%s",string);
add a comment |
C has no way to evalutate the contents of a string as an expression. You may be able to do what you want with pointers, however:
int i=1; //forexample
struct kerdesek_type *kelem = &kerdesek[index];
char *string = 0;
if(i==1){
melyikkerdes='A';
string = kelem->a_valasz;
}
else if(i==2){
melyikkerdes='B';
string = kelem->b_valasz;
}
else if(i==3){
melyikkerdes='C';
string = kelem->c_valasz;
}
else{
melyikkerdes='D';
string = kelem->d_valasz;
}
printf("%s",string);
C has no way to evalutate the contents of a string as an expression. You may be able to do what you want with pointers, however:
int i=1; //forexample
struct kerdesek_type *kelem = &kerdesek[index];
char *string = 0;
if(i==1){
melyikkerdes='A';
string = kelem->a_valasz;
}
else if(i==2){
melyikkerdes='B';
string = kelem->b_valasz;
}
else if(i==3){
melyikkerdes='C';
string = kelem->c_valasz;
}
else{
melyikkerdes='D';
string = kelem->d_valasz;
}
printf("%s",string);
answered Nov 25 '18 at 7:28
Chris DoddChris Dodd
81.8k681160
81.8k681160
add a comment |
add a comment |
If I understand you correctly, you don't want to print
kerdesek[index].a_valasz
but rather what is contained in the structure element?
You cannot construct C code as strings at runtime and have them an executable part of your program.
Is this what you are trying to do?
#include <stdio.h>
#define MAX_RECORDS 2
struct ker
{
char a_answer[50];
char b_answer[50];
};
int main()
{
struct ker questions[MAX_RECORDS] = {{"a_one", "b_one"},{"a_two", "b_two"}};
char *reply_string = "None";
int answer_number = 2; //Example
int question_number = 1; //Example
switch (answer_number){
case 1:
reply_string = questions[question_number].a_answer;
break;
case 2:
reply_string = questions[question_number].b_answer;
break;
default:
reply_string = "None";
break;
}
printf("%sn", reply_string);
return 0;
}
add a comment |
If I understand you correctly, you don't want to print
kerdesek[index].a_valasz
but rather what is contained in the structure element?
You cannot construct C code as strings at runtime and have them an executable part of your program.
Is this what you are trying to do?
#include <stdio.h>
#define MAX_RECORDS 2
struct ker
{
char a_answer[50];
char b_answer[50];
};
int main()
{
struct ker questions[MAX_RECORDS] = {{"a_one", "b_one"},{"a_two", "b_two"}};
char *reply_string = "None";
int answer_number = 2; //Example
int question_number = 1; //Example
switch (answer_number){
case 1:
reply_string = questions[question_number].a_answer;
break;
case 2:
reply_string = questions[question_number].b_answer;
break;
default:
reply_string = "None";
break;
}
printf("%sn", reply_string);
return 0;
}
add a comment |
If I understand you correctly, you don't want to print
kerdesek[index].a_valasz
but rather what is contained in the structure element?
You cannot construct C code as strings at runtime and have them an executable part of your program.
Is this what you are trying to do?
#include <stdio.h>
#define MAX_RECORDS 2
struct ker
{
char a_answer[50];
char b_answer[50];
};
int main()
{
struct ker questions[MAX_RECORDS] = {{"a_one", "b_one"},{"a_two", "b_two"}};
char *reply_string = "None";
int answer_number = 2; //Example
int question_number = 1; //Example
switch (answer_number){
case 1:
reply_string = questions[question_number].a_answer;
break;
case 2:
reply_string = questions[question_number].b_answer;
break;
default:
reply_string = "None";
break;
}
printf("%sn", reply_string);
return 0;
}
If I understand you correctly, you don't want to print
kerdesek[index].a_valasz
but rather what is contained in the structure element?
You cannot construct C code as strings at runtime and have them an executable part of your program.
Is this what you are trying to do?
#include <stdio.h>
#define MAX_RECORDS 2
struct ker
{
char a_answer[50];
char b_answer[50];
};
int main()
{
struct ker questions[MAX_RECORDS] = {{"a_one", "b_one"},{"a_two", "b_two"}};
char *reply_string = "None";
int answer_number = 2; //Example
int question_number = 1; //Example
switch (answer_number){
case 1:
reply_string = questions[question_number].a_answer;
break;
case 2:
reply_string = questions[question_number].b_answer;
break;
default:
reply_string = "None";
break;
}
printf("%sn", reply_string);
return 0;
}
answered Nov 25 '18 at 8:19
GermanNerdGermanNerd
512111
512111
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464884%2fc-printing-the-actual-content-of-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Could you add example input and output?
– J...S
Nov 25 '18 at 5:25
from my sturct (kerdesek[index].a_valasz) i have to print "apple", but it only prints the name of the path, not the exact string, that the path contains
– Gergely Halász
Nov 25 '18 at 5:33
You are using
endwithout initializing it.– H.S.
Nov 25 '18 at 5:35
i is hard coded to 1, then you concatenate "a_valasz" to "kerdesek[index]." giving your answer, so... it is doing what you are telling it to.
– SteveFerg
Nov 25 '18 at 5:35
sry i translated it bad. vege=end
– Gergely Halász
Nov 25 '18 at 5:36