LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-22-2018, 04:45 AM   #1
harely9
LQ Newbie
 
Registered: May 2018
Posts: 5

Rep: Reputation: Disabled
How to find length of char** ?


I tried this
Code:
sizeof(char) / sizeof(char[0])
but it always returns 1
 
Old 05-22-2018, 06:52 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
It seems very unclear what you're asking.

Please consider being more clear with your question, or providing more background about what you are looking to determine.

Your thread title and your code are different.

First off, the sizeof(char) is whatever the machine decides, but likely it is 1.

Divide that by the sizeof a zero element array and I'm unsure why you didn't get a "divide by zero" exception in your program. Unless the compiler implicitly made that a single character, where then it decided that anything divided by the same thing is always 1.

The length of a pointer is the natural length of a register on the machine you are using.

If you want the length of the pointer, then use sizeof(char *)
 
Old 05-22-2018, 09:31 AM   #3
harely9
LQ Newbie
 
Registered: May 2018
Posts: 5

Original Poster
Rep: Reputation: Disabled
I have dynamically assigned an array of strings. I try to find a length of array, which means how many strings there are in the array.
and i dont want to count
 
Old 05-22-2018, 09:49 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by harely9 View Post
I have dynamically assigned an array of strings. I try to find a length of array, which means how many strings there are in the array.
and i dont want to count
Taking that comment as literal.

This would therefore not be about memory size, but instead the literal count of strings you have added to this array.

Your code would have to count.

As you add strings to the array, you just bump the count of strings variable up by 1 each time.

If you add some huge chunk, or multiple huge chunks, either each chunk counts as 1, or you need to search each chunk to derive the number of strings included, such as search for newline characters and count them.

Or ...

Do you really mean that you want the entire size of this large array you have built up?

There are a number of ways to build an array of strings, you could have pre-allocated a static memory chunk, you could be building it up piece by piece as you get new strings.

Can you post a representative code clip which illustrates how you are building this array?
 
Old 05-22-2018, 03:09 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP: use a variable to store the number of used elements:
Code:
int used= 0;
int total= 0;
char **ptr= NULL;
...
if (used==total) {
    total+=10;
    ptr= realloc (ptr, total*sizeof(char *));
}
ptr[used]= strdup(input);
++used;

Last edited by NevemTeve; 05-22-2018 at 03:10 PM.
 
Old 06-15-2018, 01:31 PM   #6
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
It does not work like that as sizeof pointer over size of pointer is of course always 1.

I suppose you saw the sizeof(something)/sizeof(something[0]) somewhere which can help you access the max number items in that array. It only works when the type size is known at compile time.

Code:
  item_t item[360];

...

  size_t max_size = sizeof(item)/sizeof(item[0]); /* yields 360 */
If your array is dynamic, then you will have to keep count.

Also, char** is not array of string. It is pointer to pointer to char (or handle to char).
 
Old 06-15-2018, 02:12 PM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by harely9 View Post
I have dynamically assigned an array of strings. I try to find a length of array, which means how many strings there are in the array.
and i dont want to count
Actually I do not understand you. You either need to store the length of the array somewhere or count it. I couldn't imagine any other way.
So if you have it somewhere just use, otherwise count it.
Since you gave no information about how it was created/constructed hard to say more, but probably there are some tricks....
 
Old 06-15-2018, 02:19 PM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Well it's kind of a half month old question where the OP didn't clarify or post any representative code.
 
  


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
how to find array length in C golden_boy615 Programming 5 07-03-2011 10:27 AM
finding the length of an char array, containing 0's (as ints, not chars) Aquarius_Girl Programming 14 02-24-2010 11:55 PM
how can I find the length of all radio button? rblampain Programming 3 08-12-2007 09:35 PM
Find Length of char * array in C++ burninGpi Programming 6 08-08-2006 08:53 AM
Shell scripting to find length of filenames ridertech Linux - Newbie 2 08-25-2004 12:07 PM

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

All times are GMT -5. The time now is 08:38 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