Re: array of pointers



rp <sindica@xxxxxxxxx> wrote:
> I am having problems with array of pointers. Could someone tell me
> whats the problem with my program, shown below.

Whould be better if you would tell what problems you experience...

> #include <stdio.h>
> #include <string.h>

> #define NUM_KEYWORDS 3

> char *table[NUM_KEYWORDS] = { "abcdef",
> "ghijkl",
> "mnopqr"
> };

All the three pointers point to literal strings, i.e. strings in
in memory that doesn't belong to you and that could be in read-
only memory (some systems let you get away with changing them, but
that's nothing you can count on). That's probably the worst of your
problems. If you want to change the memory the pointers are pointing
to you must initialize them to point to memory you own (e.g. by allo-
cating memory for the strings) and then copy the strings there.

> void capitalize(char *str) {
> int size = strlen(str);
> int i=0;
> for(i=0; i < size; i++) {
> if((str[i] >= 0x61) && (str[i] <= 0x7a))
> str[i] = str[i] - 0x20;

This will only work with ASCII. Why not use the standard toupper()
function? Doesn't cost you more than including <ctype.h> and will
work with every encoding and even will safe you a couple of key
strokes;-) And why don't you use at least character constants like
'a' and 'z' instead of 0x61 and 0x7a? That only makes your program
harder to read without any positive effects.

> }
> }

> int main() {

You better make that "int main(void)" to indicate what arguments the
function takes.

> int i;
> for(i=0;i<NUM_KEYWORDS; i++) {

Add here e.g.

char *tmp;
if ( ( tmp = malloc( strlen( table[i] ) + 1 ) ) == NULL )
return EXIT_FAILURE;
table[i] = strcpy( tmp, table[i] );

to allocate memory and copy the strings. Don't forget to include
<stdlib.h> where malloc() is decared.

> capitalize(table[i]);
> printf("Capitalized keywords %s\n", table[i]);

Now you better don't forget to get rid of the memory you allocted:

free( tmp );

> }
> return 1;
> }

When your return value is supposed to indicate success make that it
either 0 or EXIT_SUCCESS (and use EXIT_FAILURE for failure), that's
the only portable return codes.
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@xxxxxxxxxxxxxxxxxxx
\__________________________ http://www.toerring.de
.



Relevant Pages

  • Re: A taxonomy of types
    ... however, elsewhere in my project (off in the dynamic typesystem, ...), I ... (since I am using NULL-terminated strings), and so I have used U+10FFFF ... remember, C also has things like arrays, funtion pointers, nestable ... int RIL_TypeSmallIntP; ...
    (comp.lang.misc)
  • Re: OK, Im stumped, couldnt free string pointer list...
    ... by alpha order of "substrings" at certain locations in the strings. ... the list of string pointers! ... If you write outside the area of a memory you allocated ...
    (comp.lang.c)
  • Re: Array of Pointers
    ... > If you are repeatedly searching for strings within a large file ... Needles are usually smaller than haystacks. ... It is known that the file is likely already in memory cache. ... strings and set the pointers, which will be used later to perform the ...
    (comp.lang.c)
  • Re: malloc and pointer hell
    ... >array (pointers to pointers). ... This allocates space for only one pointer. ... place is an int**. ... you no longer have access to the memory that was ...
    (comp.lang.c)
  • Re: problem with fwrite/fread and writing a struct
    ... That doesn't make sense - you need memory for a complete structure because ... another thing you should keep in mind is that mallocas well as strdup() ... you're writing the pointers to your strings ...
    (comp.unix.programmer)