473,379 Members | 1,388 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,379 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 1516
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.