24 Replies - 2466 Views - Last Post: 20 December 2010 - 11:36 AM
#1
How to use strlen on a structure?
Posted 19 December 2010 - 09:38 AM
struct Student
{ char *familyName;
};
Student list[400];
and suppose I have to find the length of the string a structure like
list[1].*familyName , how do I use strlen ?
Replies To: How to use strlen on a structure?
#2
Re: How to use strlen on a structure?
Posted 19 December 2010 - 09:47 AM
So list[1] is a Student structure. Its familyName member is addressed just like a member of any structure:
list[1].familyName
So, use
strlen(list[1].familyName);
#3
Re: How to use strlen on a structure?
Posted 19 December 2010 - 09:54 AM
list[1].familyName="Micca"
I want to make M to -1 and a to -1
. Since dereference in C is illegal
#4
Re: How to use strlen on a structure?
Posted 19 December 2010 - 09:58 AM
If it doesn't work you can post again & show what you tried (and what the error message was).
#5
Re: How to use strlen on a structure?
Posted 19 December 2010 - 10:10 AM
#include <stdio.h>
int main(void)
{
char *name="Fareeha";
*(name)='c';
printf("%c",*(name));
return 0;
}
it crashes
#6
Re: How to use strlen on a structure?
Posted 19 December 2010 - 10:17 AM
If you WANT to edit a strin gliteral then you have to copy it into some buffer either allocated dynamically or in an array:
#include <stdio.h>
int main(void)
{
char name[100] = "Fareeha";
*name='c';
printf("%s",name);
return 0;
}
or
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *name = (char*)malloc(100);
strcpy(name, "Fareeha");
*name='c';
printf("%s",name);
return 0;
}
#7
Re: How to use strlen on a structure?
Posted 19 December 2010 - 10:18 AM
If you want a string that you can alter, you have to allocate a char array and then you can assign characters to it and alter them as much as you want.
eg:
char name[] = "Fareeha";
This post has been edited by r.stiltskin: 19 December 2010 - 10:19 AM
#8
Re: How to use strlen on a structure?
Posted 19 December 2010 - 10:32 AM
The last question , in the part b , it says if the familyName in the one of array of student structures. I think I am reading the question wrong. What do you mean by both indices set to -1?>
#9
Re: How to use strlen on a structure?
Posted 19 December 2010 - 10:54 AM
Do you understand now?
Edit: However, I think that it's a poorly-worded question. The first index of any name in a string is 0, so that's not a very useful piece of information. Since the names are contained in an array of Student structures, I would think that the function should use one of its parameters to report the index of the particular Student in the Student array, and another parameter to report either the last index of the name, or the length of the name. So I don't really know how to answer that question. If I were given that on an exam I would ask the professor to clarify or correct the question.
This post has been edited by r.stiltskin: 19 December 2010 - 11:13 AM
#10
Re: How to use strlen on a structure?
Posted 19 December 2010 - 11:31 AM
r.stiltskin, on 19 December 2010 - 09:54 AM, said:
Do you understand now?
Do you mean that when the caller function checks the familyname of each of the sorted arrays, it checks the whole family name or just the first and last index ? O__O Or does it mean we are only to search the first and the last index of the given sorted array list's family Name. And if it cant find it, its gonna return -1.
r.stiltskin, on 19 December 2010 - 09:54 AM, said:
Do you understand now?
Edit: However, I think that it's a poorly-worded question. The first index of any name in a string is 0, so that's not a very useful piece of information. Since the names are contained in an array of Student structures, I would think that the function should use one of its parameters to report the index of the particular Student in the Student array, and another parameter to report either the last index of the name, or the length of the name. So I don't really know how to answer that question. If I were given that on an exam I would ask the professor to clarify or correct the question.
So I am right to find this question confusing , lol, ohh well I hope whoever makes the paper this time doesnt screw me up with a weird question. And the bad part is even if you ask them to clarify it to u during exam, they dont give a proper answer =___=
#11
Re: How to use strlen on a structure?
Posted 19 December 2010 - 12:06 PM
micaxxa, on 19 December 2010 - 01:31 PM, said:
Let's try to be as precise as possible. A function can only return 1 value (or none). In this case, since two values are called for, it should probably not return anything (i.e. its return type should be void), and any info it produces should be stored in parameters. So to answer this question I might write a function with this signature:
void find_name( char* name, int* idxA, int* idxB, Student* array);
The question is, what should idxA and idxB represent?
Clearly the function must attempt to match the entire name to the familyName of one of the Student objects in the array. It would be nonsensical to match only the first and last letters.
If I read the question literally, whenever a name cannot be matched, the function will assign -1 to both *idxA and *idxB. But whenever the name is exactly matched, the function will assign 0 to *idxA, and strlen(name) - 1 to *idxB, neither of which is very useful.
Since the calling function already knows (or can easily determine) the length of the search name, the only useful information that is needed is the index of the corresponding student in the Student array. So I think a more sensible function would look like one of these:
int find_name( char* name, Student* array );
or
void find_name( char* name, int* idx, Student* array );
When the name is found it would return the index in the Student array or assign it to *idx, and when the name is not found it would either return -1 or assign -1 to *idx. But that's not what the question asks for.
Edit: By the way, if I were you I would be writing to the professor RIGHT NOW asking about this question to try and head off a similar problem on your exam.
This post has been edited by r.stiltskin: 19 December 2010 - 12:13 PM
#12
Re: How to use strlen on a structure?
Posted 19 December 2010 - 01:05 PM
My professor doesnt reply to me, I am going to meet him off if I can tomorrow. Thanks alot for you time trying to explain a question . Another question ,
when I define a structure like that in the question, the char *familyName, is there a requirement for memory allocation using malloc? . I mean I used char arrays inside a structure but never char *.
This post has been edited by JackOfAllTrades: 19 December 2010 - 02:06 PM
Reason for edit:: Removed unnecessary quote
#13
Re: How to use strlen on a structure?
Posted 19 December 2010 - 02:22 PM
micaxxa, on 19 December 2010 - 03:05 PM, said:
It's a bit strange that you mentioned malloc. If you're learning C++ you should be using new for dynamic allocation, not malloc.
But either way the answer to your question is "probably" (or "maybe"). It depends on where the names are coming from. If string literals are somehow being assigned to those pointers, then they already have their own memory allocated to them. But it's hard to imagine why this program would have a selection of string literals to assign. More likely the names would be input from the keyboard or read in from a file so each name would be stored in a temporary buffer (char array), but the contents of that array will change as each succeeding name is read in. So after each read, an array of appropriate size would be allocated to a familyName or firstName pointer in a Student struct and the new name would be copied into that new array.
I suppose there's another stupid possibility -- which may be what the writer of that question was envisioning. There could conceivably be a "big" array of names stored contiguously, one after another, something like:
'k' 'i' 'n' 'g' '\0' 'j' 'o' 'h' 'n' '\0' 'l' 'e' 'w' 'i' 's' '\0' 'm' 'a' 'r' 'y' ...
If that array was declared big enough to accomodate all the names then it would not be necessary use dynamic allocation, and each pointer could be assigned the beginning address of the appropriate name.
Also, if the program had an array like that, then a search function that returned indices to the beginning and end of a name would make sense. (But the question didn't describe anything like that array.)
#14
Re: How to use strlen on a structure?
Posted 19 December 2010 - 03:19 PM
r.stiltskin, on 19 December 2010 - 01:22 PM, said:
micaxxa, on 19 December 2010 - 03:05 PM, said:
It's a bit strange that you mentioned malloc. If you're learning C++ you should be using new for dynamic allocation, not malloc.
But either way the answer to your question is "probably" (or "maybe"). It depends on where the names are coming from. If string literals are somehow being assigned to those pointers, then they already have their own memory allocated to them. But it's hard to imagine why this program would have a selection of string literals to assign. More likely the names would be input from the keyboard or read in from a file so each name would be stored in a temporary buffer (char array), but the contents of that array will change as each succeeding name is read in. So after each read, an array of appropriate size would be allocated to a familyName or firstName pointer in a Student struct and the new name would be copied into that new array.
I suppose there's another stupid possibility -- which may be what the writer of that question was envisioning. There could conceivably be a "big" array of names stored contiguously, one after another, something like:
'k' 'i' 'n' 'g' '\0' 'j' 'o' 'h' 'n' '\0' 'l' 'e' 'w' 'i' 's' '\0' 'm' 'a' 'r' 'y' ...
If that array was declared big enough to accomodate all the names then it would not be necessary use dynamic allocation, and each pointer could be assigned the beginning address of the appropriate name.
Also, if the program had an array like that, then a search function that returned indices to the beginning and end of a name would make sense. (But the question didn't describe anything like that array.)
I mentioned malloc because I am studying C programming, but ohh well. Sorry I am a bit more confused with your reply now :S, but hey thanks for the help. n.n
#15
Re: How to use strlen on a structure?
Posted 19 December 2010 - 03:43 PM
micaxxa, on 19 December 2010 - 05:19 PM, said:
The exam that you are studying uses C++ code, not C. The array of Student structures in that example was created using C++ syntax. In C, it would be
struct Student list[400];
If you tell me which part of my answer was confusing I'll try to explain it better.
|
|

New Topic/Question
Reply




MultiQuote




|