J
JNLSeb
Here are 2 samples and after the samples are some questions.
--------------------------------
Sample 1 - Initializes the array
--------------------------------
#include "stdio.h"
#include "string.h"
#include "windows.h"
int main(int argc, char **argv) {
char *asArgs[28] = {"ABB", "aaaa"};
int v_i = 0;
for(v_i=0; v_i < 27; ++v_i){
asArgs[v_i] = malloc(100);
memset(asArgs[v_i], '\0', 100);
}
strcpy(asArgs[25], "P");
asArgs[25] = "P";
asArgs[25] ="";
asArgs[25] ="A";
strcpy(asArgs[25], "P"); //*******Will cause Fault on this 2nd call
to the string copy command.
return 0;
}
--------------------------------
Sample 2 - Does NOT initialize the array
--------------------------------
#include "stdio.h"
#include "string.h"
#include "windows.h"
int main(int argc, char **argv) {
char *asArgs[28] = {"ABB", "aaaa"};
int v_i = 0;
strcpy(asArgs[25], "P"); //******Will cause Fault on this 1st call
to the string copy command.
//*******The following commands all work.
asArgs[25] = "P";
asArgs[25] ="";
asArgs[25] ="A";
return 0;
}
Questions:
------------
1 - With Sample1, why does the first strcpy work, but the 2nd one cause the
fault?
2 - With Sample2, why does the strcpy fail?
3 - With Sample2, why can the asArgs elements be assigned values directly
when the array elements are not allocated?
4 - Is there a "proper" way to assign values to the elements of a char
array?
TIA!!!
--------------------------------
Sample 1 - Initializes the array
--------------------------------
#include "stdio.h"
#include "string.h"
#include "windows.h"
int main(int argc, char **argv) {
char *asArgs[28] = {"ABB", "aaaa"};
int v_i = 0;
for(v_i=0; v_i < 27; ++v_i){
asArgs[v_i] = malloc(100);
memset(asArgs[v_i], '\0', 100);
}
strcpy(asArgs[25], "P");
asArgs[25] = "P";
asArgs[25] ="";
asArgs[25] ="A";
strcpy(asArgs[25], "P"); //*******Will cause Fault on this 2nd call
to the string copy command.
return 0;
}
--------------------------------
Sample 2 - Does NOT initialize the array
--------------------------------
#include "stdio.h"
#include "string.h"
#include "windows.h"
int main(int argc, char **argv) {
char *asArgs[28] = {"ABB", "aaaa"};
int v_i = 0;
strcpy(asArgs[25], "P"); //******Will cause Fault on this 1st call
to the string copy command.
//*******The following commands all work.
asArgs[25] = "P";
asArgs[25] ="";
asArgs[25] ="A";
return 0;
}
Questions:
------------
1 - With Sample1, why does the first strcpy work, but the 2nd one cause the
fault?
2 - With Sample2, why does the strcpy fail?
3 - With Sample2, why can the asArgs elements be assigned values directly
when the array elements are not allocated?
4 - Is there a "proper" way to assign values to the elements of a char
array?
TIA!!!