473,320 Members | 1,853 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.

C - noob question

I'm stuck on this little code, does anyone know what's wrong?

#define COUNTOF(x) \
( sizeof(x) / sizeof((x)[0]) )

char *afalbeto[] = {
"CHAR",
"CHARS",
"COMMON"};

char *afalbeto2[] = {
"CHAR2",
"CHARS2",
"COMMON2"};

.....

void findit(char *alf1[], char *alf2[]){
int i1t = COUNTOF(alf1);
int i2t = COUNTOF(alf2);
for (int i1 = 0;i1 < i1t;i1++) {
...
}
/*same loop for alf2*/
}


main..{
..
findit(afalbeto, afalbeto2);
findit(afalbeto2, afalbeto);
..
}

It only prints 1 code or sometimes crash :(
Dec 5 '07 #1
3 1501
weaknessforcats
9,208 Expert Mod 8TB
You cannot find the number of elements in an array by using your COUNTOF macro.

Your function is:
void findit(char *alf1[], char *alf2[]){
int i1t = COUNTOF(alf1);
int i2t = COUNTOF(alf2);
for (int i1 = 0;i1 < i1t;i1++) {
...
}
In this case, sizeof(alf1) is 4 because alf1 is a pointer. Then, sizeof((alf1)[0]) is also 4 because alf1[0] is a array. The name of an array, like alf1[0], is the address of element 0 so alf1[0] is also a pointer. Now you have 4/4 or 1.

And all arrays will now have 1 element.


C and C++ have a thing called decay of array which means that when you pass an array to a function what is passed is the address only. The number of elements has been lost.

Your solution is to dump the macro and replace it with a const int for the number of elements. Pass this int along with the array to your function and all will be well.
Dec 5 '07 #2
Thanks, it worked!

I just have one last question..


void findit(char *alf1[], char *alf2[], int total1, int total2){
int j;
char o[1024];
for (int i = 0;i < total1;i++) {
wsprintf(o, "%s/%s", alf1[i], alf2[i]);
j = dosomething(o);
doanotherthing(j);
}
..

int dosomething(const char *s) { .. }
void doanotherthing(int test) { .. }


is there a faster alternative to the wsprintf? (this code is gonna be repeated millions of time).
Dec 5 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
You apparently have arrays of C-strings already and you want them merged in a buffer:
char o[1024];
...
wsprintf(o, "%s/%s", alf1[i], alf2[i]);
So you could code your own:
Expand|Select|Wrap|Line Numbers
  1. CopyMyStuff(char* target, char* s1, char* s2)
  2. {
  3.     while(*s1)
  4.     {
  5.         *target = *s1;
  6.         ++s1;
  7.     }
  8.     while(*s2)
  9.     {
  10.        *target = *s2;
  11.        ++s2;
  12.     }
  13.     *s2 = '\0';
  14. }
  15.  
and check the time between that and wsprintf().
Dec 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dave Williams | last post by:
First off...total noob to VB. So far have learned a lot in 1.5 days and feel fairly comfortable at throwing screens up. However, I want to start writing forms that revolve around an access...
10
by: Matt Hollingsworth | last post by:
Hello, Very new to python, so a noob question. When I've written stuff in JavaScript or MEL in the past, I've always adopted the variable naming convention of using a $ as the first character...
1
by: davestrike | last post by:
I am a noob to sql and asp programming. I am working on a db for the gaming squad I am a member of. One of the pages I created is a roster list of all the squad members. Part of this roster is...
3
by: We need more power captain | last post by:
Hi, I know less than a noob, I've been asked to do some compiles in VC++ 6 without knowing too much at all. (I'm a COBOL program normally so this is all too much for me) I open VC++6, open...
8
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
2
by: Chris Dunaway | last post by:
I was looking over some asp.net code and saw this asp:Image control: <asp:Image ImageUrl="~/images/logo.gif" runat="server" ID="Image1"/> What does the tilde (~) mean in the ImageUrl...
2
by: Dan McCollick | last post by:
Hi All, Noob question that I can not seem to figure out: I am trying to implement a screenscraper to pull data from two seperate websites, here is some test code so far: public static void...
0
by: AndyW | last post by:
Hey folks. I am trying to get a soap wsdl service working and have a bit of a noob php programming question for it. I'm using PHP 5.x btw. I have written a soap server that contains a...
6
by: Lang Murphy | last post by:
I'm baaaaack... some of you answered a question I had last week. Only problem is: I'm a dope who doesn't understand most of what y'all posted. Raw noob when it comes to .Net and C#. So I'm going...
1
by: SCRIPT KEEPER | last post by:
Hello, I am a complete noob and just starting off with csharp so I apologize for my basic question. I am wanting to start powershell from inside a batch script and then to pass the powershell args...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: 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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.