array of pointers

R

rp

I am having problems with array of pointers. Could someone tell me
whats the problem with my program, shown below.

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

#define NUM_KEYWORDS 3

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


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

}
}

int main() {

int i;
for(i=0;i<NUM_KEYWORDS; i++) {
capitalize(table);
printf("Capitalized keywords %s\n", table);
}
return 1;
}
 
J

Jens.Toerring

rp said:
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 >= 0x61) && (str <= 0x7a))
str = str - 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 ) + 1 ) ) == NULL )
return EXIT_FAILURE;
table = strcpy( tmp, table );

to allocate memory and copy the strings. Don't forget to include
capitalize(table);
printf("Capitalized keywords %s\n", table);


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
 
R

rp

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

My program fail at the str = str - 0x20; assignment in
"capitalize" function. I am getting a segmentation fault at that point.
Seems like str is not my memory space anymore. How could it that
happend ?. As you had mentioned my *table[] array points to string
literals. Is that the cause?. I have used it many couple of times and
never failed on it. Is that the right way of assigning array of
pointers?
 
A

Artie Gold

rp said:
Whould be better if you would tell what problems you experience...


My program fail at the str = str - 0x20; assignment in
"capitalize" function. I am getting a segmentation fault at that point.
Seems like str is not my memory space anymore. How could it that
happend ?. As you had mentioned my *table[] array points to string
literals. Is that the cause?. I have used it many couple of times and
never failed on it. Is that the right way of assigning array of
pointers?

You're not paying attention. (It happens.)

Modifying a string literal (which is what you're doing in said line)
causes undefined behavior. A crash is but one possibility of what will
occur (and frankly, the best one).

HTH,
--ag
 
D

Default User

rp said:
My program fail at the str = str - 0x20; assignment in
"capitalize" function. I am getting a segmentation fault at that point.
Seems like str is not my memory space anymore. How could it that
happend ?. As you had mentioned my *table[] array points to string
literals. Is that the cause?. I have used it many couple of times and
never failed on it. Is that the right way of assigning array of
pointers?


http://www.eskimo.com/~scs/C-faq/q1.32.html



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top