473,385 Members | 1,766 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,385 software developers and data experts.

beginner qn..malloc

hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner
thanx in adv
gordon
Jan 23 '08 #1
12 1517
nodrogbrown wrote:
hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner
thanx in adv
gordon
You can use a loop:

for (ctr = 0; ctr < ARR_SIZE; ctr++) {
mynames[ctr] = malloc(elements);
if (!mynames[ctr]) {
handle_error();
}
else {
elements = get_next_array_size();
}
}

I hope the object and routine names are self descriptive.

Jan 23 '08 #2
nodrogbrown wrote:
hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner
Something like:

#include <stdlib.h>

enum { NumberOfNames = 42 };

int main(void)
{
char* mynames[NumberOfNames] = {NULL};

size_t stringSize = someValue;

mynames[0] = malloc( stringSize );

stringSize = someOtherValue;

mynames[1] = malloc( stringSize );

/* do stuff */

for( unsigned n = 0; n < NumberOfNames; free( mynames[n++] ) );
}

--
Ian Collins.
Jan 23 '08 #3
nodrogbrown wrote:
hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner
thanx in adv
gordon
Other people have given you one kind of solution. However, if all of
your strings come from string literals, there's a much simpler solution:

char * mynames[] =
{"mathew", "gordon_brown", "F:\gordon\docs\mycv.txt"};

You probably need the more complicated solution, but since you're a
beginner I didn't want to miss out on the possibility that the simpler
solution would meet your needs.
Jan 23 '08 #4
James Kuyper wrote:
nodrogbrown wrote:
>hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner
thanx in adv
gordon

Other people have given you one kind of solution. However, if all of
your strings come from string literals, there's a much simpler
solution:

char * mynames[] =
{"mathew", "gordon_brown", "F:\gordon\docs\mycv.txt"};

You probably need the more complicated solution, but since you're a
beginner I didn't want to miss out on the possibility that the simpler
solution would meet your needs.
He specifically says above that the sizes of the strings will not be
known at compile time.

Jan 23 '08 #5
In article <fn**********@registered.motzarella.org>,
santosh <sa*********@gmail.comwrote:
>>i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.
[...]
>>mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"
>He specifically says above that the sizes of the strings will not be
known at compile time.
He also gives an example in which they are, so he might be confused.

-- Richard
--
:wq
Jan 23 '08 #6
James Kuyper <ja*********@verizon.netwrites:
nodrogbrown wrote:
>i want to create an array of strings
[...]
>char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"
[...]
>
Other people have given you one kind of solution. However, if all of
your strings come from string literals, there's a much simpler
solution:

char * mynames[] =
{"mathew", "gordon_brown", "F:\gordon\docs\mycv.txt"};
The \g, \d, and \m in the string literal are going to cause problems.
If you want a backslash character in a string literal, you need to
double it:

"F:\\gordon\\docs\\mycv.txt"

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 23 '08 #7
On Jan 23, 12:26 pm, Keith Thompson <ks...@mib.orgwrote:
James Kuyper <jameskuy...@verizon.netwrites:
nodrogbrown wrote:
i want to create an array of strings
[...]
char * mynames[]
mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"
[...]
Other people have given you one kind of solution. However, if all of
your strings come from string literals, there's a much simpler
solution:
char * mynames[] =
{"mathew", "gordon_brown", "F:\gordon\docs\mycv.txt"};

The \g, \d, and \m in the string literal are going to cause problems.
If you want a backslash character in a string literal, you need to
double it:

"F:\\gordon\\docs\\mycv.txt"
That's actually a problem inherited from the original question, I just
cut-and-pasted it. However, I should have mentioned the problem. All
my programming for the last dozen years has been for Unix-like
systems, where filenames seldom present the same kind of problem, so I
didn't even think about it.
Jan 23 '08 #8
>I'm not sure what you mean when you say that
>"F:\\gordon\\docs\\mycv.txt" "has a chance". Is that meant to imply
that there's a chance it could fail? If so, how?
How many systems do you know of that DON'T have that file? (or,
for that matter, don't even have a F: drive?) If so, trying to
open it would fail. And there's always the chance a file open could
fail due to bad sectors, insufficient permissions, Sony rootkits, etc.

There are no defined characters for the escapes \g, \d, or \m. An
implementation might take \g to be the Google logo and \m to be the
Microsoft Windows logo, which normally aren't allowed in file names.
\d might be considered an error unless the content rating allows
material for 35+.

There are defined characters for escapes such as \r, \n, \b, and
\f, but it can get awkward putting carriage returns, newlines,
backspaces, and form feeds in file names (UNIX will let you do it,
but it's still awkward).

Jan 27 '08 #9
go***********@burditt.org (Gordon Burditt) writes:
>>I'm not sure what you mean when you say that
"F:\\gordon\\docs\\mycv.txt" "has a chance". Is that meant to imply
that there's a chance it could fail? If so, how?
I wrote the above. Stop snipping attributions.
How many systems do you know of that DON'T have that file? (or,
for that matter, don't even have a F: drive?) If so, trying to
open it would fail. And there's always the chance a file open could
fail due to bad sectors, insufficient permissions, Sony rootkits, etc.
Who said anything about opening files? The original question was
about storing strings (one of which happens to look very much like a
file name).

"F:\\gordon\\docs\\mycv.txt" is a perfectly valid string literal; I
see no way it can fail *as a string literal*.

In any case, I was asking what Joe Wright meant.
There are no defined characters for the escapes \g, \d, or \m. An
implementation might take \g to be the Google logo and \m to be the
Microsoft Windows logo, which normally aren't allowed in file names.
\d might be considered an error unless the content rating allows
material for 35+.

There are defined characters for escapes such as \r, \n, \b, and
\f, but it can get awkward putting carriage returns, newlines,
backspaces, and form feeds in file names (UNIX will let you do it,
but it's still awkward).
Of course, that was the point of changing \ to \\ several messages
upthread.

This article was written by Keith Thompson <ks***@mib.org>.
Permission to quote without attribution is explicitly denied for this
message and for every message I post here.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 27 '08 #10
Keith Thompson wrote:
>
.... snip ...
>
This article was written by Keith Thompson <ks***@mib.org>.
Permission to quote without attribution is explicitly denied for
this message and for every message I post here.
I like this.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Jan 28 '08 #11
On Wed, 23 Jan 2008 15:15:11 +0000, Richard Tobin wrote:
In article <fn**********@registered.motzarella.org>, santosh
<sa*********@gmail.comwrote:
>>>i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.
[...]
>>>mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

He specifically says above that the sizes of the strings will not be
known at compile time.

He also gives an example in which they are, so he might be confused.
He also uses some odd characters in his example. What, pray tell, does
\g do? Or \d? \m?

Jan 28 '08 #12
In article <no************@spanky.localhost.net>,
Kelsey Bjarnason <kb********@gmail.comwrote:
>>>>mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"
>>>He specifically says above that the sizes of the strings will not be
known at compile time.
>He also gives an example in which they are, so he might be confused.
>He also uses some odd characters in his example. What, pray tell, does
\g do? Or \d? \m?
I think that problem was addressed several days ago.

-- Richard

--
:wq
Jan 29 '08 #13

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

Similar topics

6
by: Atip Asvanund | last post by:
Dear sirs, I am trying to learn how to use Boehm's garbage collector: http://www.hpl.hp.com/personal/Hans_Boehm/gc/ on a Linux machine. I am a beginner, and I find its documentation inadequate....
6
by: sathyashrayan | last post by:
#include<stdio.h> #include<stdlib.h> #include<string.h> struct tree { int data; struct tree *left,*right; }; void init(struct tree *node)
11
by: rory | last post by:
Cna anyone point me in the right direction, I have a struture in my .h file: typedef struct mdata { int names; int dates; int ages; }m_Data;
5
by: Yourko | last post by:
Hi there! I`me currently trying to write some simple programs in C. For one such program i created globals.h file. In that file i defined a structure of type _client, and a pointer of that type: ...
8
by: AG | last post by:
Hello, This is my first post to this group, and on top of that I am a beginner. So please direct me to another group if this post seems out of place.... I have recently written a program which...
9
by: raam | last post by:
hi all, I trying to write an array of integers into a text file.While writing into the file it works.I used fwrite function, but when I read the same file using fread or fscanf it fails and it...
7
by: fool | last post by:
Dear group, Extract the integer value present in a given string. So I tried the following: int main(void) { int val; char *data; data = malloc(sizeof *data); if(data)
13
by: fool | last post by:
Dear group, I want to check if the given string is a number. If any value is inputed other than a number then error. The following is not the correct solution. Can some one tell me any link for...
2
by: hal | last post by:
Hi, I'm trying to make an array of pointers to 'TwoCounts' structs, where the size of the array is arraySize. Right now I'm just mallocing enough space for all the pointers to the structs, and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.