Use of pointers in a typedef struct which contains a string of characters
I want to write a function called "substitution" that passes as parameters:
- a linked list "l" which has a character on each node ("lchar").
- a string of characters "CHG"
- a character "CH"
- a "vec" structure that contains the final string.
The function scans each character at time on the linked list, and writes on "vec" the characters of the linked list which are different from the character CH passed as a parameter, but when it scans the exact CH character on the linked list, it writes on "vec" the string "CHG".
The following is the solution given by my textbook. I seem to understand the overall program, but I can't understand the passage when the function scans the character CH:
svec->v[cur]=''; strcat(svec->v,CHG);
What does it do exactly? Why are we using pointers for "svec"? Why it is written svec->v[cur]=l->lchar; since we are handling a string of characters?
Solution:
struct el {
struct el *next;
char lchar;
};
struct el *l=NULL;
typedef struct {
int size;
char *v;
} vec;
vec svec;
int substitution (struct el *l, char *CHG, char CH, vec *svec) {
\ initialize svec in the dynamic memory
int cur=0;
while (l!=NULL) {
if (l->lchar !=CH) {
svec->v[cur]=l->lchar;
cur++;
l=l->next;
} else {
svec->v[cur]='';
strcat(svec->v,CHG);
cur=cur+strlen(CHG);
l=l->next;
}
}
svec->v[cur]='';
return 1;
}
c function pointers linked-list typedef
add a comment |
I want to write a function called "substitution" that passes as parameters:
- a linked list "l" which has a character on each node ("lchar").
- a string of characters "CHG"
- a character "CH"
- a "vec" structure that contains the final string.
The function scans each character at time on the linked list, and writes on "vec" the characters of the linked list which are different from the character CH passed as a parameter, but when it scans the exact CH character on the linked list, it writes on "vec" the string "CHG".
The following is the solution given by my textbook. I seem to understand the overall program, but I can't understand the passage when the function scans the character CH:
svec->v[cur]=''; strcat(svec->v,CHG);
What does it do exactly? Why are we using pointers for "svec"? Why it is written svec->v[cur]=l->lchar; since we are handling a string of characters?
Solution:
struct el {
struct el *next;
char lchar;
};
struct el *l=NULL;
typedef struct {
int size;
char *v;
} vec;
vec svec;
int substitution (struct el *l, char *CHG, char CH, vec *svec) {
\ initialize svec in the dynamic memory
int cur=0;
while (l!=NULL) {
if (l->lchar !=CH) {
svec->v[cur]=l->lchar;
cur++;
l=l->next;
} else {
svec->v[cur]='';
strcat(svec->v,CHG);
cur=cur+strlen(CHG);
l=l->next;
}
}
svec->v[cur]='';
return 1;
}
c function pointers linked-list typedef
add a comment |
I want to write a function called "substitution" that passes as parameters:
- a linked list "l" which has a character on each node ("lchar").
- a string of characters "CHG"
- a character "CH"
- a "vec" structure that contains the final string.
The function scans each character at time on the linked list, and writes on "vec" the characters of the linked list which are different from the character CH passed as a parameter, but when it scans the exact CH character on the linked list, it writes on "vec" the string "CHG".
The following is the solution given by my textbook. I seem to understand the overall program, but I can't understand the passage when the function scans the character CH:
svec->v[cur]=''; strcat(svec->v,CHG);
What does it do exactly? Why are we using pointers for "svec"? Why it is written svec->v[cur]=l->lchar; since we are handling a string of characters?
Solution:
struct el {
struct el *next;
char lchar;
};
struct el *l=NULL;
typedef struct {
int size;
char *v;
} vec;
vec svec;
int substitution (struct el *l, char *CHG, char CH, vec *svec) {
\ initialize svec in the dynamic memory
int cur=0;
while (l!=NULL) {
if (l->lchar !=CH) {
svec->v[cur]=l->lchar;
cur++;
l=l->next;
} else {
svec->v[cur]='';
strcat(svec->v,CHG);
cur=cur+strlen(CHG);
l=l->next;
}
}
svec->v[cur]='';
return 1;
}
c function pointers linked-list typedef
I want to write a function called "substitution" that passes as parameters:
- a linked list "l" which has a character on each node ("lchar").
- a string of characters "CHG"
- a character "CH"
- a "vec" structure that contains the final string.
The function scans each character at time on the linked list, and writes on "vec" the characters of the linked list which are different from the character CH passed as a parameter, but when it scans the exact CH character on the linked list, it writes on "vec" the string "CHG".
The following is the solution given by my textbook. I seem to understand the overall program, but I can't understand the passage when the function scans the character CH:
svec->v[cur]=''; strcat(svec->v,CHG);
What does it do exactly? Why are we using pointers for "svec"? Why it is written svec->v[cur]=l->lchar; since we are handling a string of characters?
Solution:
struct el {
struct el *next;
char lchar;
};
struct el *l=NULL;
typedef struct {
int size;
char *v;
} vec;
vec svec;
int substitution (struct el *l, char *CHG, char CH, vec *svec) {
\ initialize svec in the dynamic memory
int cur=0;
while (l!=NULL) {
if (l->lchar !=CH) {
svec->v[cur]=l->lchar;
cur++;
l=l->next;
} else {
svec->v[cur]='';
strcat(svec->v,CHG);
cur=cur+strlen(CHG);
l=l->next;
}
}
svec->v[cur]='';
return 1;
}
c function pointers linked-list typedef
c function pointers linked-list typedef
asked Jan 19 at 18:20
KevinKevin
1318
1318
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
svec->v[cur]=''; strcat(svec->v,CHG);
This replace the char at [cur] position on the svec->v char* with a terminating null byte in order to use strcat on it.
From man strcat :
The strcat() function appends the src string to the dest string, overwriting the terminating
null byte ('') at the end of dest, and then adds a terminating null byte.
If the "dest char *" of strcat lacks terminating null-byte, strcat behavior is unpredictable. It serves as a security.
svec->v[cur]=l->lchar;
It is written like that because "svec" and "l" are pointers, hence, you need to deference them in order to access their data.
Hope it helps !
Thank you! Much appreciated :)
– Kevin
Jan 19 at 19:12
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%2f54270065%2fuse-of-pointers-in-a-typedef-struct-which-contains-a-string-of-characters%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
svec->v[cur]=''; strcat(svec->v,CHG);
This replace the char at [cur] position on the svec->v char* with a terminating null byte in order to use strcat on it.
From man strcat :
The strcat() function appends the src string to the dest string, overwriting the terminating
null byte ('') at the end of dest, and then adds a terminating null byte.
If the "dest char *" of strcat lacks terminating null-byte, strcat behavior is unpredictable. It serves as a security.
svec->v[cur]=l->lchar;
It is written like that because "svec" and "l" are pointers, hence, you need to deference them in order to access their data.
Hope it helps !
Thank you! Much appreciated :)
– Kevin
Jan 19 at 19:12
add a comment |
svec->v[cur]=''; strcat(svec->v,CHG);
This replace the char at [cur] position on the svec->v char* with a terminating null byte in order to use strcat on it.
From man strcat :
The strcat() function appends the src string to the dest string, overwriting the terminating
null byte ('') at the end of dest, and then adds a terminating null byte.
If the "dest char *" of strcat lacks terminating null-byte, strcat behavior is unpredictable. It serves as a security.
svec->v[cur]=l->lchar;
It is written like that because "svec" and "l" are pointers, hence, you need to deference them in order to access their data.
Hope it helps !
Thank you! Much appreciated :)
– Kevin
Jan 19 at 19:12
add a comment |
svec->v[cur]=''; strcat(svec->v,CHG);
This replace the char at [cur] position on the svec->v char* with a terminating null byte in order to use strcat on it.
From man strcat :
The strcat() function appends the src string to the dest string, overwriting the terminating
null byte ('') at the end of dest, and then adds a terminating null byte.
If the "dest char *" of strcat lacks terminating null-byte, strcat behavior is unpredictable. It serves as a security.
svec->v[cur]=l->lchar;
It is written like that because "svec" and "l" are pointers, hence, you need to deference them in order to access their data.
Hope it helps !
svec->v[cur]=''; strcat(svec->v,CHG);
This replace the char at [cur] position on the svec->v char* with a terminating null byte in order to use strcat on it.
From man strcat :
The strcat() function appends the src string to the dest string, overwriting the terminating
null byte ('') at the end of dest, and then adds a terminating null byte.
If the "dest char *" of strcat lacks terminating null-byte, strcat behavior is unpredictable. It serves as a security.
svec->v[cur]=l->lchar;
It is written like that because "svec" and "l" are pointers, hence, you need to deference them in order to access their data.
Hope it helps !
answered Jan 19 at 18:51
Antoine. FAntoine. F
262
262
Thank you! Much appreciated :)
– Kevin
Jan 19 at 19:12
add a comment |
Thank you! Much appreciated :)
– Kevin
Jan 19 at 19:12
Thank you! Much appreciated :)
– Kevin
Jan 19 at 19:12
Thank you! Much appreciated :)
– Kevin
Jan 19 at 19:12
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%2f54270065%2fuse-of-pointers-in-a-typedef-struct-which-contains-a-string-of-characters%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