Add items to an array | |
I'm trying to generate an array of serial numbers for use in an
application. I can get individual serial numbers printed to stdout using
a for loop, but I cannot figure out how to append these numbers to an
array.
Here is my code:
#include <stdio.h>
int main (void) {
int i;
int count = 20000;
char appname[] = "MYAPP";
int serials[20000];
for (i = 1; i < count; ++i) {
int five = i*5;
int eleven = i/11;
int one = (i-1);
printf("%s-%i-%i-%i\n", appname, five, eleven, one);
}
return 0;
}
In a higher-level language such as Python I'd do something like this:
serialnumber = ("%s-%i-%i-%i\n", appname, five, eleven, one)
serials.append(serialnumber)
but I'm not sure how to assign this kind of value to a C variable, nor
am I sure how to populate the C array with this value. Any advice is
appreciated.
--
Kevin Walzer
Code by Kevin http://www.codebykevin.com | | | | re: Add items to an array
Kevin Walzer <kw@codebykevin.comwrites: Quote:
I'm trying to generate an array of serial numbers for use in an
application. I can get individual serial numbers printed to stdout
using a for loop, but I cannot figure out how to append these numbers
to an array.
>
Here is my code:
>
#include <stdio.h>
>
int main (void) {
>
int i;
int count = 20000;
char appname[] = "MYAPP";
int serials[20000];
Does that number look similar to anything else in the code? .... Quote:
for (i = 1; i < count; ++i) {
int five = i*5;
int eleven = i/11;
int one = (i-1);
printf("%s-%i-%i-%i\n", appname, five, eleven, one);
}
Look at the start value of i. Read about indexing in C. Check the end
case for the loop to be sure to be sure .... Quote:
return 0;
>
>
}
>
In a higher-level language such as Python I'd do something like this:
>
serialnumber = ("%s-%i-%i-%i\n", appname, five, eleven, one)
serials.append(serialnumber)
>
but I'm not sure how to assign this kind of value to a C variable, nor
am I sure how to populate the C array with this value. Any advice is
appreciated.
Look up sprintf. | | | | re: Add items to an array
Kevin Walzer wrote: Quote:
I'm trying to generate an array of serial numbers for use in an
application. I can get individual serial numbers printed to stdout
using a for loop, but I cannot figure out how to append these numbers
to an array.
Quote:
char appname[] = "MYAPP";
int serials[20000];
Quote:
In a higher-level language such as Python I'd do something like this:
>
serialnumber = ("%s-%i-%i-%i\n", appname, five, eleven, one)
serials.append(serialnumber)
The array serials[] is one containing ints. You have created strings
with non-numeric components (like the app name). What exactly would you
expect this append() to do? You certainly haven't created an int of any
kind. You have a string. Why not an array of strings?
Brian | | | | re: Add items to an array
On Mon, 23 Jun 2008 13:43:04 -0400, Kevin Walzer wrote: Quote:
#include <stdio.h>
>
int main (void) {
>
int i;
int count = 20000;
char appname[] = "MYAPP";
int serials[20000];
You want an array of strings, so you need:
char serials[2000][22];
Here you should replace 22 with one more than the maximum size of your
serial string. If you don't know that in advance, then you need to go
away and learn dynamic memory allocation (start with man malloc). Some
will say that having a 44kB automatic variable is a bad idea in any case,
(and they would be right) so you should eventually learn how to allocate
memory anyway. Quote:
for (i = 1; i < count; ++i) {
int five = i*5;
int eleven = i/11;
int one = (i-1);
printf("%s-%i-%i-%i\n", appname, five, eleven, one);
snprintf( serials[i], 22, "%s-%i-%i-%i\n",
appname, five, eleven, one);
You should also check that the return value of snprintf is less than 22,
otherwise your string has been truncated by the fixed size buffer. include <stdlib.hand use
exit( EXIT_SUCCESS ); or exit( EXIT_FAILURE ); HTH
viza | | | | re: Add items to an array
On Jun 23, 11:06 pm, viza <tom.v...@gmil.comwrote: Quote:
On Mon, 23 Jun 2008 13:43:04 -0400, Kevin Walzer wrote:
<snip> Quote: >
include <stdlib.hand use
>
exit( EXIT_SUCCESS ); or exit( EXIT_FAILURE );
> There isn't a problem with 'return 0', and there's a subtle difference
between 'return' and exit(). Perhaps OP desires that? | | | | re: Add items to an array
On Mon, 23 Jun 2008 13:22:23 -0700, vippstar wrote: Quote:
On Jun 23, 11:06 pm, viza <tom.v...@gmil.comwrote: Quote:
>On Mon, 23 Jun 2008 13:43:04 -0400, Kevin Walzer wrote:
<snip> Quote: >>
>include <stdlib.hand use
>>
> exit( EXIT_SUCCESS ); or exit( EXIT_FAILURE );
>> There isn't a problem with 'return 0', and there's a subtle difference
between 'return' and exit(). Perhaps OP desires that?
There isn't a problem with 'return EXIT_SUCCESS', but hard coding values
(1) makes them difficult to find and change (2) ignores an opportunity to
make the code easier to read. | | | | re: Add items to an array
On Jun 24, 12:30 am, viza <tom.v...@gmil.comwrote: Quote:
On Mon, 23 Jun 2008 13:22:23 -0700, vippstar wrote: Quote:
On Jun 23, 11:06 pm, viza <tom.v...@gmil.comwrote: Quote:
On Mon, 23 Jun 2008 13:43:04 -0400, Kevin Walzer wrote:
<snip> > Quote: Quote:
include <stdlib.hand use
> Quote: Quote:
exit( EXIT_SUCCESS ); or exit( EXIT_FAILURE );
> Quote: There isn't a problem with 'return 0', and there's a subtle difference
between 'return' and exit(). Perhaps OP desires that?
>
There isn't a problem with 'return EXIT_SUCCESS', but hard coding values
(1) makes them difficult to find and change (2) ignores an opportunity to
make the code easier to read.
'0' is not more or less of a hardcoded value than EXIT_SUCCESS in
'return' | | | | re: Add items to an array
'0' is not more or less of a hardcoded value than EXIT_SUCCESS in Quote:
'return'- Alıntıyı gizle -
>
- Alıntıyı göster -
in *more* system EXIT_SUCCESS = 0 and EXIT_FAILURE = 1. You are
absolutely right. | | | | re: Add items to an array
On Jun 24, 12:58 am, Ali Karaali <ali...@gmail.comwrote: Quote: Quote:
'0' is not more or less of a hardcoded value than EXIT_SUCCESS in
'return'
in *more* system EXIT_SUCCESS = 0 and EXIT_FAILURE = 1. You are
absolutely right.
Actually the value of these two macros have nothing to do with 0 being
'hardcoded'.
I don't know what 'viza' is talking about, but 0 is not more or less
hardcoded than EXIT_SUCCESS/EXIT_FAILURE, regardless of the value of
the expressions the macros expand to. | | | | re: Add items to an array
On Mon, 23 Jun 2008 15:02:19 -0700, vippstar wrote: Quote:
On Jun 24, 12:58 am, Ali Karaali <ali...@gmail.comwrote: Quote: Quote:
>>On Mon, 23 Jun 2008 21:30:58 +0000, viza wrote:
Quote: Quote: Quote:
>>>There isn't a problem with 'return EXIT_SUCCESS', but hard coding
>>>values (1) makes them difficult to find and change (2) ignores an
>>>opportunity to make the code easier to read.
Quote: Quote: Quote:
>>'0' is not more or less of a hardcoded value than EXIT_SUCCESS in
>>'return' in *more* system EXIT_SUCCESS = 0 and EXIT_FAILURE = 1.
Quote:
Actually the value of these two macros have nothing to do with 0 being
'hardcoded'.
Quote:
I don't know what 'viza' is talking about, but 0 is not more or less
hardcoded than EXIT_SUCCESS/EXIT_FAILURE, regardless of the value of the
expressions the macros expand to.
"Hard coded" wasn't the right term, but using literal constant values (as
opposed to a macro) is still a bad idea, because:
(1) it makes them difficult to find and change (eg when porting)
(2) it ignores an opportunity to make the code easier to read.
Only (2) is really significant here, but that is enough reason to use the
macro. | | | | re: Add items to an array
"Kevin Walzer" <kw@codebykevin.comwrote in message
news:3f8bd$485fe02c$4275d90a$21110@FUSE.NET... Quote:
I'm trying to generate an array of serial numbers for use in an
application. I can get individual serial numbers printed to stdout using a
for loop, but I cannot figure out how to append these numbers to an array.
>
Here is my code:
>
#include <stdio.h>
>
int main (void) {
>
int i;
int count = 20000;
char appname[] = "MYAPP";
int serials[20000];
for (i = 1; i < count; ++i) {
int five = i*5;
int eleven = i/11;
int one = (i-1);
printf("%s-%i-%i-%i\n", appname, five, eleven, one);
}
return 0;
Any programming text will explain about arrays.
The C code should look more like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define count 20000
int main (void) {
int i,n;
char sno[100];
char appname[] = "MYAPP";
char *serials[count+1]; /* allow 1-based indexing */
char *p;
for (i = 1; i < count; ++i) {
int five = i*5;
int eleven = i/11;
int one = (i-1);
sprintf(sno,"%s-%i-%i-%i", appname, five, eleven, one);
n=strlen(sno);
p=malloc(n+1);
if (p==NULL) { puts("No more memory"); exit(0);}
strcpy(p,sno);
serials[i]=p;
}
for (i = 1; i < count; ++i)
printf("%d: %s\n",i,serials[i]);
return 0;
}
Each serial number string is allocated separately. You could simplify a bit
by allocating a fixed size (say 30 chars) per serial string. (If the serial
numbers are really generated like this, they don't need to be stored as a
string at all; just generated as needed by a function.) Quote:
In a higher-level language such as Python I'd do something like this:
>
serialnumber = ("%s-%i-%i-%i\n", appname, five, eleven, one)
Presumably there's something missing here? Quote:
serials.append(serialnumber)
So why are you switching to C?
-- Bartc | | | | re: Add items to an array
On 23 Jun, 23:02, vipps...@gmail.com wrote: Quote:
On Jun 24, 12:58 am, Ali Karaali <ali...@gmail.comwrote:>
Quote: Quote: Quote:
>'0' is not more or less of a hardcoded value than EXIT_SUCCESS in
'return'
I assume you meant "'0' is no more or less a hardcoded value than
EXIT_SUCCESS
in 'return".
I disagree. Quote: Quote:
in *more* system EXIT_SUCCESS = 0 and EXIT_FAILURE = 1. You are
absolutely right.
do you mean "most systems"?
I'm not being pedantic here, I'm simply not sure what you mean. Quote:
Actually the value of these two macros have nothing to do with 0 being
'hardcoded'.
I don't know what 'viza' is talking about, but 0 is not more or less
hardcoded than EXIT_SUCCESS/EXIT_FAILURE, regardless of the value of
the expressions the macros expand to.
Ok, so why do I disagree? There are a few reasons to hide hard-coded
values.
1. to document the code
2. to make the value easy to find and change
3. to make it different from other constants that take the same value,
at the moment.
most people think of 2 and hence regard "return 0" as perfectly clear.
It is also fine by rule 1. But may fall astray of rule 3.
How many zeros can there be in your code and can any of them change
NULL_POINTER, NUL_CHARACTER, INDEX_OFFSET, MONDAY
actually I was making those up. <pause for grep>
#define I_MIN_IDENT 0
#define xxx_NO_PID 0
#define LOG_EMERG 0
#define xxx_ALARMS_ALL_CLEAR 0
#define xxx_LOCK_REST_OF_SEGMENT 0
#define MIN_STATUS_NO 0
#define DUMMY_ROW_NUMBER 0
#define SACREDMEM 0
#define NO_CLIENT 0
#define xxx_SUCCESS 0
#define PARENT 0
#define NONE 0
#define NULL_OPPA 0
#define HEAD_OFFSET 0
#define TEST_INT_ZERO 0
#define HIGH_QUALITY 0
#define QUEUES_OFF 0
[only a sample (there were lots). Slightly edited to disguise source]
Can you (or I!) guarantee that *none* of those will ever change?
But also a confession. I regard zero (and 1) as slight exceptions.
I will write stuff like this:
/* remove '\n' */
str[nl_posn] = 0;
int main(void)
{
do_stuff();
return 0;
}
in C I use NULL. In C++ I use 0. It's a cultural thing.
--
Nick Keighley | | | | re: Add items to an array
On Jun 24, 12:03 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote: Quote:
On 23 Jun, 23:02, vipps...@gmail.com wrote:
> Quote:
On Jun 24, 12:58 am, Ali Karaali <ali...@gmail.comwrote:> Quote:
'0' is not more or less of a hardcoded value than EXIT_SUCCESS in
'return'
>
I assume you meant "'0' is no more or less a hardcoded value than
EXIT_SUCCESS
in 'return".
Yes, that is what I meant. I know my English isn't very good, but I
think I'm understandable with little effort :) Quote:
I disagree.
> Quote: Quote:
in *more* system EXIT_SUCCESS = 0 and EXIT_FAILURE = 1. You are
absolutely right.
>
do you mean "most systems"?
Just a note here: These are *not* my words, these are Mr Karaali's
words. Quote:
I'm not being pedantic here, I'm simply not sure what you mean.
> Quote:
Actually the value of these two macros have nothing to do with 0 being
'hardcoded'.
I don't know what 'viza' is talking about, but 0 is not more or less
hardcoded than EXIT_SUCCESS/EXIT_FAILURE, regardless of the value of
the expressions the macros expand to.
>
Ok, so why do I disagree? There are a few reasons to hide hard-coded
values.
Actually if you disagree you're wrong. Both EXIT_SUCCESS and 0 are
hardcoded values.
EXIT_SUCCESS offers nothing more over 0 whether you realize it or not.
It's not possible to redefine EXIT_SUCCESS to a value other
EXIT_SUCCESS or 0. Quote:
1. to document the code
2. to make the value easy to find and change
3. to make it different from other constants that take the same value,
at the moment.
>
most people think of 2 and hence regard "return 0" as perfectly clear.
It is also fine by rule 1. But may fall astray of rule 3.
>
How many zeros can there be in your code and can any of them change
>
NULL_POINTER, NUL_CHARACTER, INDEX_OFFSET, MONDAY
>
actually I was making those up. <pause for grep>
>
#define I_MIN_IDENT 0
#define xxx_NO_PID 0
#define LOG_EMERG 0
#define xxx_ALARMS_ALL_CLEAR 0
#define xxx_LOCK_REST_OF_SEGMENT 0
#define MIN_STATUS_NO 0
#define DUMMY_ROW_NUMBER 0
#define SACREDMEM 0
#define NO_CLIENT 0
#define xxx_SUCCESS 0
#define PARENT 0
#define NONE 0
#define NULL_OPPA 0
#define HEAD_OFFSET 0
#define TEST_INT_ZERO 0
#define HIGH_QUALITY 0
#define QUEUES_OFF 0
>
[only a sample (there were lots). Slightly edited to disguise source]
>
Can you (or I!) guarantee that *none* of those will ever change?
No, but I can guarantee EXIT_SUCCESS will not be redefined to a value
other than 0 or EXIT_SUCCESS. Quote:
But also a confession. I regard zero (and 1) as slight exceptions.
>
I will write stuff like this:
>
/* remove '\n' */
str[nl_posn] = 0;
>
int main(void)
{
do_stuff();
return 0;
>
}
>
in C I use NULL. In C++ I use 0. It's a cultural thing.
How is any of that relevant to EXIT_SUCCESS and 0 being both hardcoded
values? | | | | re: Add items to an array
On Jun 24, 6:00 am, vipps...@gmail.com wrote: Quote:
On Jun 24, 12:03 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:On 23 Jun, 23:02, vipps...@gmail.com wrote:
> Quote: Quote:
On Jun 24, 12:58 am, Ali Karaali <ali...@gmail.comwrote:>
>'0' is not more or less of a hardcoded value than EXIT_SUCCESS in
'return'
> Quote:
I assume you meant "'0' is no more or less a hardcoded value than
EXIT_SUCCESS
in 'return".
>
Yes, that is what I meant. I know my English isn't very good, but I
think I'm understandable with little effort :)I disagree.
> Quote: Quote:
in *more* system EXIT_SUCCESS = 0 and EXIT_FAILURE = 1. You are
absolutely right.
> Quote:
do you mean "most systems"?
>
Just a note here: These are *not* my words, these are Mr Karaali's
words.I'm not being pedantic here, I'm simply not sure what you mean.
> Quote: Quote:
Actually the value of these two macros have nothing to do with 0 being
'hardcoded'.
I don't know what 'viza' is talking about, but 0 is not more or less
hardcoded than EXIT_SUCCESS/EXIT_FAILURE, regardless of the value of
the expressions the macros expand to.
> Quote:
Ok, so why do I disagree? There are a few reasons to hide hard-coded
values.
>
Actually if you disagree you're wrong. Both EXIT_SUCCESS and 0 are
hardcoded values.
EXIT_SUCCESS offers nothing more over 0 whether you realize it or not.
It's not possible to redefine EXIT_SUCCESS to a value other
EXIT_SUCCESS or 0.
>
Yes, they're both hard-coded values. But EXIT_SUCCESS *does* offer
things that 0 doesn't: readability, self-documenting code, reusable
code, portability, among other things. Some of this points don't seem
so apparent in this particular case, plus 'return 0;' is almost like a
tradition. But in general, using macros rather than constants is
better in all senses. Otherwise, why would they have been invented in
the first place? | | | | re: Add items to an array s0suk3@gmail.com wrote: Quote:
On Jun 24, 6:00 am, vipps...@gmail.com wrote:
Quote: Quote:
>Actually if you disagree you're wrong. Both EXIT_SUCCESS and 0 are
>hardcoded values. EXIT_SUCCESS offers nothing more over 0 whether you
>realize it or not. It's not possible to redefine EXIT_SUCCESS to a
>value other EXIT_SUCCESS or 0.
By not possible, I think you mean not portable, right? Quote:
Yes, they're both hard-coded values. But EXIT_SUCCESS *does* offer
things that 0 doesn't: readability,
Disputable. Literally every C programmer knows what a "return 0;" from
main means. It usually in one of the first chapters in every C book. Quote:
self-documenting code, reusable
code, portability, among other things.
And how is a literal zero return from main (or in an exit call) not
these? Quote:
Some of this points don't seem
so apparent in this particular case, plus 'return 0;' is almost like a
tradition. But in general, using macros rather than constants is
better in all senses. Otherwise, why would they have been invented in
the first place?
I think in this case EXIT_SUCCESS was created for symmetry rather than
any real need. But your general point of using macros instead of
literal constants where feasible is quite correct. I just don't think
that this case (0 or EXIT_SUCCESS) is a great example of illustrating
the advantages of macros. :-) | | | | re: Add items to an array
On 24 Jun, 12:00, vipps...@gmail.com wrote: Quote:
On Jun 24, 12:03 pm,Nick Keighley<nick_keighley_nos...@hotmail.com>
wrote:On 23 Jun, 23:02, vipps...@gmail.com wrote: Quote: Quote:
On Jun 24, 12:58 am, Ali Karaali <ali...@gmail.comwrote:>
<snip> Quote: Quote:
I assume you meant "'0' is no more or less a hardcoded value than
EXIT_SUCCESS in 'return".
>
Yes, that is what I meant. I know my English isn't very good, but I
think I'm understandable with little effort :)I disagree.
> Quote: Quote:
in *more* system EXIT_SUCCESS = 0 and EXIT_FAILURE = 1. You are
absolutely right.
> Quote:
do you mean "most systems"?
>
Just a note here: These are *not* my words, these are Mr Karaali's
words.
Yes, I know. That's what the little arrows ( ) indicate.
I use a single post to make multiple points. Quote: Quote:
I'm not being pedantic here, I'm simply not sure what you mean.
<snip>
--
Nick keighley | | | | re: Add items to an array
On Fri, 27 Jun 2008 10:28:48 +0000, Richard Bos wrote: Quote:
viza <tom.viza@gmil.comwrote:
> Quote:
>On Mon, 23 Jun 2008 15:02:19 -0700, vippstar wrote: Quote:
I don't know what 'viza' is talking about, but 0 is not more or less
hardcoded than EXIT_SUCCESS/EXIT_FAILURE, regardless of the value of
the expressions the macros expand to.
>>
>"Hard coded" wasn't the right term, but using literal constant values
>(as opposed to a macro) is still a bad idea, because:
>>
>(1) it makes them difficult to find and change (eg when porting)
>>
>(2) it ignores an opportunity to make the code easier to read.
>
I'd agree with you in almost any other case, but in this case the
meaning of 0 is explicitly mentioned in the Standard.
Ok, that takes away (1), but not everyone has read the standard.
Everyone can read and understand EXIT_SUCCESS. | | | | re: Add items to an array
viza <tom.viza@gmil.comwrites: Quote:
On Fri, 27 Jun 2008 10:28:48 +0000, Richard Bos wrote: Quote:
viza <tom.viza@gmil.comwrote: Quote:
On Mon, 23 Jun 2008 15:02:19 -0700, vippstar wrote:
I don't know what 'viza' is talking about, but 0 is not more or less
hardcoded than EXIT_SUCCESS/EXIT_FAILURE, regardless of the value of
the expressions the macros expand to.
>
"Hard coded" wasn't the right term, but using literal constant values
(as opposed to a macro) is still a bad idea, because:
>
(1) it makes them difficult to find and change (eg when porting)
>
(2) it ignores an opportunity to make the code easier to read.
I'd agree with you in almost any other case, but in this case the
meaning of 0 is explicitly mentioned in the Standard.
>
Ok, that takes away (1), but not everyone has read the standard.
Everyone can read and understand EXIT_SUCCESS.
Everyone who knows C sufficiently well can read and understand
``exit(0);'' or ``return 0;''.
--
Keith Thompson (The_Other_Keith) kst-u@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" | | | | re: Add items to an array
viza <tom.viza@gmil.comwrote: Quote:
On Fri, 27 Jun 2008 10:28:48 +0000, Richard Bos wrote:
> Quote:
viza <tom.viza@gmil.comwrote: Quote:
On Mon, 23 Jun 2008 15:02:19 -0700, vippstar wrote:
I don't know what 'viza' is talking about, but 0 is not more or less
hardcoded than EXIT_SUCCESS/EXIT_FAILURE, regardless of the value of
the expressions the macros expand to.
>
"Hard coded" wasn't the right term, but using literal constant values
(as opposed to a macro) is still a bad idea, because:
>
(1) it makes them difficult to find and change (eg when porting)
>
(2) it ignores an opportunity to make the code easier to read.
I'd agree with you in almost any other case, but in this case the
meaning of 0 is explicitly mentioned in the Standard.
>
Ok, that takes away (1), but not everyone has read the standard.
No, but this is elementary. Anyone who doesn't know about a 0 return
value from main() cannot call himself a C programmer yet.
Richard | | | | re: Add items to an array
Richard Bos wrote: Quote:
viza <tom.viza@gmil.comwrote: Quote:
>Richard Bos wrote:
>>
.... snip ... Quote: Quote:
>> Quote:
>>I'd agree with you in almost any other case, but in this case
>>the meaning of 0 is explicitly mentioned in the Standard.
>>
>Ok, that takes away (1), but not everyone has read the standard.
>
No, but this is elementary. Anyone who doesn't know about a 0
return value from main() cannot call himself a C programmer yet.
And there is no excuse for not at least skimming the standard. It
is easily available. See the sig below.
--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://c-faq.com/ (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf(C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2(C99, txt)
<http://www.dinkumware.com/c99.aspx (C-library}
<http://gcc.gnu.org/onlinedocs/ (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction> |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|