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_bro wn"
mynames[2]="F:\gordon\doc s\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 12 1546
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_bro wn"
mynames[2]="F:\gordon\doc s\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.
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_bro wn"
mynames[2]="F:\gordon\doc s\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.
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_bro wn"
mynames[2]="F:\gordon\doc s\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_bro wn", "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.
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_bro wn" mynames[2]="F:\gordon\doc s\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_bro wn", "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.
In article <fn**********@r egistered.motza rella.org>,
santosh <sa*********@gm ail.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_bro wn" mynames[2]="F:\gordon\doc s\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
James Kuyper <ja*********@ve rizon.netwrites :
nodrogbrown wrote:
>i want to create an array of strings
[...]
>char * mynames[]
mynames[0]="mathew" mynames[1]="gordon_bro wn" mynames[2]="F:\gordon\doc s\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_bro wn", "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\\do cs\\mycv.txt"
--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
On Jan 23, 12:26 pm, Keith Thompson <ks...@mib.orgw rote:
James Kuyper <jameskuy...@ve rizon.netwrites :
nodrogbrown wrote:
i want to create an array of strings
[...]
char * mynames[]
mynames[0]="mathew"
mynames[1]="gordon_bro wn"
mynames[2]="F:\gordon\doc s\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_bro wn", "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\\do cs\\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.
>I'm not sure what you mean when you say that
>"F:\\gordon\\d ocs\\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). go***********@b urditt.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\\do cs\\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_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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. I have followed
all instructions for installation, yet I still have the following
questions.
1) The instruction said to include the header file, and link to the
library file. I was wondering if I am doing it correctly in my
|
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)
|
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;
|
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:
struct _client{
int fd; // file descriptor
struct sockaddr_in sock_name;
} * client;
Later in that program i do:
client = (struct _client *) malloc(sizeof(struct _client));
|
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 calculates loan
amortization schedules, writes the data to a text file, and then upon
user prompt, the program will display the created file and print the
file. Until this morning, everything worked fine, and then I started
messing around with...
| |
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 prints
some junk.I am not opening the file in binary mode, there are no
characters which will terminate the fscanf function operation.How to
write and read the integer array .
Thanks in advance.
|
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)
|
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 the above task? Pls just
don't give the code. Thanks.
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
|
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 mallocing space
for the pointer 'countPtr' in each struct, but do I need to do
anything else? Thanks.
typedef struct TwoCounts {
int *countPtr;
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |