473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

char**

MN
I have a question :
How to understand the mean of char** type ?
Aug 30 '08 #1
16 1605
On Aug 30, 6:01*am, MN <mazouz.nezh...@gmail.comwrote:
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
Aug 30 '08 #2
MN said:
I have a question :
How to understand the mean of char** type ?
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
Aug 30 '08 #3
MN
On Aug 30, 2:55 pm, Richard Heathfield <r...@see.sig.invalidwrote:
MN said:
I have a question :
How to understand the mean of char** type ?

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.
Aug 30 '08 #4
MN wrote:
On Aug 30, 2:55 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>MN said:
>>I have a question :
How to understand the mean of char** type ?
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.
I don't understand what you mean.

Does your function create the array
and then the calling function changes the size of the array,
or
does your function receive the address of an array as a argument
and then change the size of it?

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.

--
pete
Aug 30 '08 #5

"MN" <ma************@gmail.comwrote in message
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.
Multidimensional arrays aren't implemented well in C.
One way of doing them is to declare a 2d array

char array[10][10];

but this is fixed. The other way is to declare a list of pointers to
pointers

char **array = malloc(10 * sizeof(char *));
for(i=0;i<10;i++)
array[i] = malloc(10 * sizeof(char));

these two representations have very little in common with each other.

Another way is to allocate a 1d array and manage it manually

char *array = malloc(10 * 10 * sizeof(char));

/* set x, y, to the letter F */
array[y*10+x] = 'F';

You need to find out which method your function is using, which is probably
the second.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 30 '08 #6
MN <ma************@gmail.comwrites:
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/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 30 '08 #7
On Sat, 30 Aug 2008 03:32:04 -0700 (PDT), zz****@gmail.com wrote:
>On Aug 30, 6:01*am, MN <mazouz.nezh...@gmail.comwrote:
>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,
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].

--
Remove del for email
Aug 31 '08 #8
On Sat, 30 Aug 2008 10:55:21 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>MN said:
>I have a question :
How to understand the mean of char** type ?

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).
>
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?
--
Remove del for email
Aug 31 '08 #9
On Sat, 30 Aug 2008 05:07:54 -0700 (PDT), MN
<ma************@gmail.comwrote:
>On Aug 30, 2:55 pm, Richard Heathfield <r...@see.sig.invalidwrote:
snip
>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
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[i][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].

--
Remove del for email
Aug 31 '08 #10
MN
On Aug 31, 5:19 am, Barry Schwarz <schwa...@dqel.comwrote:
On Sat, 30 Aug 2008 05:07:54 -0700 (PDT), 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[i][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

Aug 31 '08 #11
Barry Schwarz <sc******@dqel.comwrites:
On Sat, 30 Aug 2008 10:55:21 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>>MN said:
>>I have a question :
How to understand the mean of char** type ?

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).
[...]

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".)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 31 '08 #12
On Sun, 31 Aug 2008 01:23:20 -0700, Keith Thompson wrote:
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.
Aug 31 '08 #13
Harald van Dijk <tr*****@gmail.comwrites:
On Sun, 31 Aug 2008 01:23:20 -0700, Keith Thompson wrote:
>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.
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 31 '08 #14

"MN" <ma************@gmail.comwrote in message
>
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[i] = 0;
for(i=0;i<height;i++)
{
answer[i] = malloc( (width + 1) * sizeof(char ));
if(!answer[i])
goto error_exit;
for(ii=0;ii<width;ii++)
answer[i][ii] = 'a';
/* a a terminal NUL */
answer[i][width] = 0;
}
return answer;
/* out of memory, clean up */
error_exit:
if(answer)
{
for(i=0;i<height;i++)
free(answer[i]);
free(answer);
}
return 0;
}

int main(void)
{
char **array;
int i;

array = dynarray(10, 12);
if(array == 0)
{
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(array[2], "Fred");
array[3][0] = '*';
for(i=0;i<10;i++)
printf("%s\n", array[i]);
return 0;
}

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 31 '08 #15
On Sun, 31 Aug 2008 01:23:20 -0700, Keith Thompson <ks***@mib.org>
wrote:
>Barry Schwarz <sc******@dqel.comwrites:
>On Sat, 30 Aug 2008 10:55:21 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>>>MN said:

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

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).
[...]

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.


--
Remove del for email
Aug 31 '08 #16
On Aug 30, 11:01*am, MN <mazouz.nezh...@gmail.comwrote:
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)++;

}
Aug 31 '08 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.