didgerman said:
didgerman said:
Chaps,
I need to properly format the case of a struct. Can I just hit it with
tolower, and then 'while (string [pos]==' ')
pos++;
string[pos]=toupper(string[pos]); to add in the higher case for the
start of each letter?
The struct will contain some integers, will tolower/upper affect any
integers?
[...]
Right, getting somewhere here.
How can I use a for loop on a struct? Without using all the struct
members.....
That's sort of like asking how you can drive a nail using a
screwdriver. You should probably just be asking how to drive a nail.
Are you trying to iterate over the members of a struct? You can't.
(Actually, you probably can if you first build an array each element
of which contains offset and size information for the struct members
you're interested in, but that's almost certainly more effort than
it's worth.)
The toupper() and tolower() functions apply to a single character:
char c = some_value;
c = toupper(c);
To map a string to upper or lower case, you can use a loop to iterate
over the characters of the array:
char *s = "hello, world";
char *ptr;
for (ptr = s; *ptr != '\0'; ptr ++) {
*ptr = toupper(*ptr);
}
or, if you're more comfortable with array indexing rather than pointer
arithmetic:
char *s = "hello, world";
int i;
for (i = 0; s
!= '\0'; i ++) {
s = toupper(s);
}
You can encapsulate the loop by putting it into a function that takes
a pointer to a string and maps the string to upper case.
If you have a struct some of whose members are character arrays
containing string values, and you want to map each such member to
upper case, the best approach is just to explicitly map each member:
struct my_struct_type {
int x;
char name[MAX_NAME_LEN];
int y;
char str[SOME_OTHER_VALUE];
char c;
} my_struct_object;
map_string_to_upper(my_struct_object.name);
map_string_to_upper(my_struct_object.str);
my_struct_object.c = toupper(my_struct_object.c);
There are still a lot of possible complications. Are the members
you're dealing with character arrays or character pointers? If
they're arrays, are they nul-terminated strings or just arbitrary
arrays of characters; do you want to iterate over the entire array, or
just up to a terminating '\0' character?