LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-31-2007, 04:25 PM   #1
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Rep: Reputation: 37
array.length in C problem


Hey there
I am trying to make a fuction in C which converts an array of chars to a list of characters.
This is what i get
Quote:
wordADT.c: In function `buildWord':
wordADT.c:17: error: request for member `length' in something not a structure or union
What i trying to do is to access each element of that array.
My code looks like that.

//my fuction body. It takes as a parameter char str[]
int i;
for (i=0; i<str.length; i++)
{
//do something
}

Any ideas?
Thanks for your time.
 
Old 01-31-2007, 04:31 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Please put code in [code] tags to improve readability.

char* values (like str in your example) don't have member functions. You should read up about the basics of data types in C. Also, you're using C++ style comments - do you want to use the C++ string class, or C char pointers?

In C, you'd probably want to be using the strlen function, assuming your char is null-terminated. In C++ you might want to use the string class.
 
Old 01-31-2007, 07:47 PM   #3
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
You're a Java programmer, aren't you?

In C, arrays aren't aware of their length. The workarounds for this are:

1) Use a sentinel value to indicate where the array ends. This is the convention that's used for strings. Iterating through a null-terminated string is done with:

Code:
char* p;

for (p = str; *p != '\0'; ++p) {
   /* do something */
}
2) For each array used as a function parameter, pass the array's length in another parameter. For example,

Code:
int main(int argc, char* argv[])
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Object array length ArcLinux Programming 2 11-04-2006 08:43 AM
Find Length of char * array in C++ burninGpi Programming 6 08-08-2006 08:53 AM
tcl newbie: can't count length of array jimieee Programming 12 06-03-2006 12:38 PM
finding array length from a reference (in Perl) lowpro2k3 Programming 1 06-22-2005 04:49 PM
finding the length of an array in perl acid_kewpie Programming 5 09-25-2002 10:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration