Re: Please explain this issue with array of char pointers.
- From: user923005 <dcorbit@xxxxxxxxx>
- Date: Tue, 22 Jan 2008 16:42:50 -0800 (PST)
On Jan 22, 4:20 pm, "JNLSeb" <jnl...@xxxxxxxxxxxxx> wrote:
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;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main0 (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"); /* Fault on 2nd call to strcpy function.
*/
return 0;
}
int main1 (int argc, char **argv)
{
char *asArgs[28] =
{"ABB", "aaaa"};
int v_i = 0;
strcpy (asArgs[25], "P"); /* Fault on 1st call to strcpy function */
asArgs[25] = "P";
asArgs[25] = "";
asArgs[25] = "A";
return 0;
}
int main (int argc, char **argv)
{
main0 (argc, argv);
main1 (argc, argv);
return 0;
}
/*
C:\tmp>splint foo.c
Splint 3.1.1 --- 12 Mar 2007
foo.c: (in function main0)
foo.c(8,3): Initializer block for asArgs has 2 elements, but declared
as char *
[28]: "ABB", "aaaa"
Initializer does not define all elements of a declared array. (Use
-initallelements to inhibit warning)
foo.c(13,28): Function memset expects arg 2 to be int gets char: '\0'
A character constant is used as an int. Use +charintliteral to allow
character constants to be used as ints. (This is safe since the
actual type
of a char constant is int.)
foo.c(13,15): Possibly null storage asArgs[] passed as non-null param:
memset (asArgs[v_i], ...)
A possibly null pointer is passed as a parameter corresponding to a
formal
parameter with no /*@null@*/ annotation. If NULL may be used for
this
parameter, add a /*@null@*/ annotation to the function parameter
declaration.
(Use -nullpass to inhibit warning)
foo.c(12,21): Storage asArgs[] may become null
foo.c(16,3): Observer storage assigned to unqualified reference:
asArgs[25] = "P"
Observer storage is transferred to a non-observer reference. (Use
-observertrans to inhibit warning)
foo.c(16,16): Storage becomes observer
foo.c(17,3): Observer storage assigned to unqualified reference:
asArgs[25] = ""
foo.c(17,16): Storage becomes observer
foo.c(18,3): Observer storage assigned to unqualified reference:
asArgs[25] = "A"
foo.c(18,16): Storage becomes observer
foo.c(5,16): Parameter argc not used
A function parameter is not used in the body of the function. If the
argument
is needed for type compatibility or future plans, use /*@unused@*/
in the
argument declaration. (Use -paramuse to inhibit warning)
foo.c(5,29): Parameter argv not used
foo.c: (in function main1)
foo.c(27,3): Initializer block for asArgs has 2 elements, but declared
as char
* [28]: "ABB", "aaaa"
foo.c(32,3): Observer storage assigned to unqualified reference:
asArgs[25] = "P"
foo.c(32,16): Storage becomes observer
foo.c(33,3): Observer storage assigned to unqualified reference:
asArgs[25] = ""
foo.c(33,16): Storage becomes observer
foo.c(34,3): Observer storage assigned to unqualified reference:
asArgs[25] = "A"
foo.c(34,16): Storage becomes observer
foo.c(28,7): Variable v_i declared but not used
A variable is declared but never used. Use /*@unused@*/ in front of
declaration to suppress message. (Use -varuse to inhibit warning)
foo.c(24,16): Parameter argc not used
foo.c(24,29): Parameter argv not used
foo.c: (in function main)
foo.c(41,3): Return value (type int) ignored: main0(argc, argv)
Result returned by function call is not used. If this is intended,
can cast
result to (void) to eliminate message. (Use -retvalint to inhibit
warning)
foo.c(42,3): Return value (type int) ignored: main1(argc, argv)
foo.c(5,5): Function exported but not used outside foo: main0
A declaration is exported, but not used outside this module.
Declaration can
use static qualifier. (Use -exportlocal to inhibit warning)
foo.c(22,1): Definition of main0
foo.c(24,5): Function exported but not used outside foo: main1
foo.c(37,1): Definition of main1
Finished checking --- 19 code warnings
*/
Questions:
------------
1 - With Sample1, why does the first strcpy work, but the 2nd one cause the
fault?
What do you think?
2 - With Sample2, why does the strcpy fail?
What do you think and why?
3 - With Sample2, why can the asArgs elements be assigned values directly
when the array elements are not allocated?
Tell me what you think about it?
4 - Is there a "proper" way to assign values to the elements of a char
array?
Yes.
TIA!!!
WIA!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.
- References:
- Prev by Date: Re: string question
- Next by Date: Re: linked list question
- Previous by thread: Re: Please explain this issue with array of char pointers.
- Next by thread: Re: Please explain this issue with array of char pointers.
- Index(es):
Relevant Pages
|