How to use strlen on a structure?

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2466 Views - Last Post: 20 December 2010 - 11:36 AM Rate Topic: -----

#1 micaxxa  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 17-October 10

How to use strlen on a structure?

Posted 19 December 2010 - 09:38 AM

If I had a structure like this

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 ?
Is This A Good Question/Topic? 0
  • +

Replies To: How to use strlen on a structure?

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How to use strlen on a structure?

Posted 19 December 2010 - 09:47 AM

list is an array of Student structures. list[1] is the second element in that array (remember, array elements are numbered beginning with 0).

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);

Was This Post Helpful? 1
  • +
  • -

#3 micaxxa  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: How to use strlen on a structure?

Posted 19 December 2010 - 09:54 AM

Thanks , that cleared alot . I have another question how do you modify the first and last index of family.Name of a a given student structure e.g
list[1].familyName="Micca"

I want to make M to -1 and a to -1



. Since dereference in C is illegal
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How to use strlen on a structure?

Posted 19 December 2010 - 09:58 AM

Why don't you see if you can figure that out yourself & write some code.

If it doesn't work you can post again & show what you tried (and what the error message was).
Was This Post Helpful? 0
  • +
  • -

#5 micaxxa  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 17-October 10

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
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: How to use strlen on a structure?

Posted 19 December 2010 - 10:17 AM

This crashes because "Fareeha" is a "string literal" and is therefore placed into a constant data segment. i.e. name should really be declared as a "const char*" to remeind you not to try and edit it.

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;
}


Was This Post Helpful? 1
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How to use strlen on a structure?

Posted 19 December 2010 - 10:18 AM

That's because "Fareeha" is a string literal. When you create a string literal, you are given a pointer to it, which you can use to "read" its value, but you are never allowed to alter the contents of the string.

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

Was This Post Helpful? 0
  • +
  • -

#8 micaxxa  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: How to use strlen on a structure?

Posted 19 December 2010 - 10:32 AM

I am just confused because I wanted to try something like this but I am doing an exam question http://courses.skule...f_2006_exam.pdf

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?>
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How to use strlen on a structure?

Posted 19 December 2010 - 10:54 AM

That question doesn't involve changing the names. It's about writing a search function that will search through an array of Student structures looking for a particular name, and provide the calling function with information about what it finds. The indices to be set are parameters of the search function.

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

Was This Post Helpful? 0
  • +
  • -

#10 micaxxa  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: How to use strlen on a structure?

Posted 19 December 2010 - 11:31 AM

View Postr.stiltskin, on 19 December 2010 - 09:54 AM, said:

That question doesn't involve changing the names. It's about writing a search function that will search through an array of Student structures looking for a particular name, and provide the calling function with information about what it finds. The indices to be set are parameters of the search function.

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.

View Postr.stiltskin, on 19 December 2010 - 09:54 AM, said:

That question doesn't involve changing the names. It's about writing a search function that will search through an array of Student structures looking for a particular name, and provide the calling function with information about what it finds. The indices to be set are parameters of the search function.

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 =___=
Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How to use strlen on a structure?

Posted 19 December 2010 - 12:06 PM

View Postmicaxxa, on 19 December 2010 - 01:31 PM, said:

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.

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

Was This Post Helpful? 0
  • +
  • -

#12 micaxxa  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: How to use strlen on a structure?

Posted 19 December 2010 - 01:05 PM

I found this int find_name( char* name, Student* array ); much more understandable.
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

Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How to use strlen on a structure?

Posted 19 December 2010 - 02:22 PM

View Postmicaxxa, on 19 December 2010 - 03:05 PM, said:

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 *.

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.)
Was This Post Helpful? 0
  • +
  • -

#14 micaxxa  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: How to use strlen on a structure?

Posted 19 December 2010 - 03:19 PM

View Postr.stiltskin, on 19 December 2010 - 01:22 PM, said:

View Postmicaxxa, on 19 December 2010 - 03:05 PM, said:

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 *.

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
Was This Post Helpful? 0
  • +
  • -

#15 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How to use strlen on a structure?

Posted 19 December 2010 - 03:43 PM

View Postmicaxxa, on 19 December 2010 - 05:19 PM, said:

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

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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2