char**

Z

zzt256

I have a question :
How to understand the mean of  char** type ?

Well, it's a pointer to a pointer to a char. So if char * can
represent a string, char ** points to multiple strings. For example,
to define an array of strings one would usually:
char *arr[] = { "one", "two", "three" };

If you want more clarification you need to be more specific in your
post :p
 
M

MN

MN said:


char is a type - objects of that type are 1 byte in size and can contain as
their value any single member of the execution character set.

char * is a type - objects of that type are sizeof(char *) bytes in size,
and can contain as their value the address of a single char.

char ** is a type - objects of that type are sizeof(char **) bytes in size,
and can contain as their value the address of a single char *.

What is the question behind the question?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

the question is :
I have a function which returns an array of chars with 2 dimensions.
The size of array is dynamically changed by malloc.
To return this array I must declare it as an extern in the header file
like this: extern char** array.
if I use extern char* array, I get error.
 
K

Keith Thompson

MN said:
I have a question :
How to understand the mean of char** type ?

In your later followup, you said you had a function returning an
array. (Actually, a function can't directly return an array.)

Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
 
B

Barry Schwarz

Well, it's a pointer to a pointer to a char. So if char * can
represent a string, char ** points to multiple strings. For example,

A variable of type char* cannot represent a string. It can however
point to a char which is a character in a string (usually the first
character).
to define an array of strings one would usually:
char *arr[] = { "one", "two", "three" };

While this code is correct, arr does not have type char**. It has
type array of three pointers to char which is expressed syntactically
as char *[3].
 
B

Barry Schwarz

MN said:


char is a type - objects of that type are 1 byte in size and can contain as
their value any single member of the execution character set.

char is an integer type and an object of that type can contain any
value between CHAR_MIN and CHAR_MAX, inclusive (independent of whether
the value represents a member of the execution character set).
 
B

Barry Schwarz


Please don't quote signatures (the previous 5 lines).
the question is :
I have a function which returns an array of chars with 2 dimensions.
The size of array is dynamically changed by malloc.
To return this array I must declare it as an extern in the header file
like this: extern char** array.
if I use extern char* array, I get error.

I assume you meant dynamically allocated instead of changed.

The return type for your function depends on the method you use to
allocate the array.

One popular method is to dynamically allocate space for a number
of pointers to char (corresponding to each row of your array). Then
allocate space for each of those pointers to hold the number of
characters in each row. This will allow you to refer to an array
element using normal subscript notation of the form array[j]. In
this case, you would indeed return a char** (which would point to the
first allocated space).

Another popular method is to compute the total amount of space
needed for the array and allocate it in a single block. You then
refer to the j-th element in the i-th row with syntax of the form
array[i*number_of_columns+j]. In this case, your function would
return a char* which would point to element [0][0].
 
M

MN

One popular method is to dynamically allocate space for a number
of pointers to char (corresponding to each row of your array). Then
allocate space for each of those pointers to hold the number of
characters in each row. This will allow you to refer to an array
element using normal subscript notation of the form array[j]. In
this case, you would indeed return a char** (which would point to the
first allocated space).



What I want is to use this method. Can you give a small function's
code example?
Thanks for your help
 
K

Keith Thompson

Barry Schwarz said:
char is an integer type and an object of that type can contain any
value between CHAR_MIN and CHAR_MAX, inclusive (independent of whether
the value represents a member of the execution character set).
[...]

If I'm reading C99 5.2.1 correctly, the execution character set *is*
the complete set of values from CHAR_MIN to CHAR_MAX. (This is
distinct from the "basic execution character set".)
 
H

Harald van Dijk

If I'm reading C99 5.2.1 correctly, the execution character set *is* the
complete set of values from CHAR_MIN to CHAR_MAX. (This is distinct
from the "basic execution character set".)

The execution character set may also contain multi-byte characters, so the
set of values from CHAR_MIN to CHAR_MAX cannot be the complete set. I am
not sure what is, though.
 
K

Keith Thompson

Harald van Dijk said:
The execution character set may also contain multi-byte characters, so the
set of values from CHAR_MIN to CHAR_MAX cannot be the complete set. I am
not sure what is, though.

I believe you're right. But the range of values in the range CHAR_MIN
to CHAR_MAX is a subset of the execution character set. (Any other
members are local-specific.)
 
B

Barry Schwarz

Barry Schwarz said:
char is an integer type and an object of that type can contain any
value between CHAR_MIN and CHAR_MAX, inclusive (independent of whether
the value represents a member of the execution character set).
[...]

If I'm reading C99 5.2.1 correctly, the execution character set *is*
the complete set of values from CHAR_MIN to CHAR_MAX. (This is
distinct from the "basic execution character set".)

I wonder. Consider:

1 - If char is signed, then a variable of type char can
obviously hold a negative value.

2 - Functions like fgetc should be able to handle the entire
execution character set.

3 - fgetc will treat any character it reads as unsigned and
will return only a non-negative value (promoted to int).

Therefore, it seems there are char values which are not part
of the execution character set.
 
B

Ben Bacarisse

Malcolm McLean said:
MN said:
What I want is to use this method. Can you give a small function's
code example?
Thanks for your help
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char **dynarray(int width, int height)
{
char **answer;
int i, ii;

answer = malloc(height * sizeof(char *));
if(!answer)
goto error_exit;
for(i=0;i<height;i++)
answer = 0;


Personally, I think this needs a comment! I know why you do it, but
would the typical learner see why you set all the answer to NULL
before entering the loop that sets them.

To the OP: it is there so that the loop that frees the pointers in the
error_exit code will never try to free an indeterminate pointer.
for(i=0;i<height;i++)
{
answer = malloc( (width + 1) * sizeof(char ));
if(!answer)
goto error_exit;
for(ii=0;ii<width;ii++)
answer[ii] = 'a';
/* a a terminal NUL */
answer[width] = 0;
}
return answer;
/* out of memory, clean up */
error_exit:
if(answer)
{
for(i=0;i<height;i++)
free(answer);
free(answer);
}
return 0;
}


Still to the OP: I am not a fan of goto, so I would write:

char **dynarray(int width, int height)
{
char **answer = malloc(height * sizeof answer[0]);
if (answer) {
int i = 0;
while (i < height && (answer = malloc(width + 1)) != NULL)
answer[i++][0] = 0;
if (i == height)
return answer;
while (i > 0)
free(answer[--i]);
free(answer);
}
return 0;
}

which, to me, requires less checking. I would also rather swap the
two arguments to make them match the size order when declaring arrays
in C.
 
B

Bart

I have a question :
How to understand the mean of  char** type ?

Here's an example not related to arrays. Probably not very useful in
this form, better when stepping through words etc.

In this case, char** is simply a pointer to char* (eg. a pointer to a
string.)

#include <stdio.h>

char nextchar(char**);

int main(void)
{char *s="One Two Three";
char *t;
char c;

t=s; /* Initialise pointer to step through string */

while (c=nextchar(&t)) printf("%c",c);
puts("");
}

/* Step through string *p; return next character in string, update
caller's pointer until end-of-string */
char nextchar(char **p){

if (**p==0) return 0;

return *(*p)++;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top