C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old June 19th, 2002, 03:25 AM
nucleuz nucleuz is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 8 nucleuz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
arrays and pointers

Ok: I'm having issues with pointers and multidimensional arrays.
I need to decide on run-time which array to use.
In : syntax.h:
char *c_syntax[2] = {"word", "test" };
char *d_syntax[2] = {"c", "php" };
(add 18 more arrays here)

In : file.c:
Code:
#include <stdio.h>
#include "syntax.h"
int main()
{
    int i;
    int j;
    /* 
     * Need to point 'syntax' to a double array (decided on run-time) 
     * I have 10-20 arrays that *syntax can point to
     * This is where it causes problems: 
     */
    char *syntax;
    syntax = c_syntax; /* just for testing purposes*/
    
    printf("Starting test\n");
    /* 
     * Loop through and print everything in c_syntax 
     * If I use c_syntax in the loop here the code works
     */
    for(i = 0; i < 2; i++){
        for(j = 0; j < strlen(syntax[i]); j++){
            printf("%c", syntax[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Now: what can I do to just work with the 'syntax' pointer instead of 20 different arrays in my program??

Thanks for any help on this issue, I've just started with C, pointeres normally don't confuse me, but this has bugged me a while now.

Reply With Quote
  #2  
Old June 19th, 2002, 05:07 PM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 37 m 7 sec
Reputation Power: 9
Send a message via ICQ to Analyser
First off, since c_syntax is an array of pointer to char (char *c_syntax[]), syntax should be either a pointer to pointer to char (char **syntax), or an array of pointer to char (char *syntax[]).

Here's an example that uses a single 'syntax' array:

Code:
#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char **argv)
{
        char *syntax[4][2] = {
                { "word", "test" },
                { "c", "php" },
                { "c++", "java" },
                { "python", "perl" }
        };

        int i;

        for (i = 0; i < 4; i++) {
                        printf("%s and %s\n", syntax[i][0], syntax[i][1]);
        }

        return 0;
}


I hope this makes sense. If not, ask away
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/

Reply With Quote
  #3  
Old June 20th, 2002, 01:42 AM
nucleuz nucleuz is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 8 nucleuz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks!!
I'm getting there now, I have the arrays in the header file going smooth (a couple of them are 2500 variables+)
So: how would I go about to malloc all these? I know how to parse stuff in C, but memory management is *not* my best side.

Reply With Quote
  #4  
Old June 20th, 2002, 03:20 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 37 m 7 sec
Reputation Power: 9
Send a message via ICQ to Analyser
I'm not quite following you, there. If syntax.h contains array declarations like c_syntax, with initial values and all, then there's no sense in using malloc() on them.

If you're not initialising them in the header file, then you'll need to populate the arrays yourself. Assuming declarations like char *c_syntax[2], you end up arrays of pointer-to-char. That means you only need to allocate space for the actual strings; the array declarations create room for the pointers.

For example:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 2

int
main (int argc, char **argv)
{
        char *c_syntax[SIZE];
        char buf[8192]; /* should be enough for most strings :) */
        int i;

        for (i = 0; i < SIZE; i++) {
                fgets(buf, sizeof(buf), stdin); /* should check return code */
                c_syntax[i] = malloc(strlen(buf) + 1); /* again, should check for errors */
                strcpy(c_syntax[i], buf);
        }

        /* print array in reverse */
        for (i = SIZE; i > 0; i--) {
                printf("%s", c_syntax[i-1]);
        }

        return 0;
}


Instead of using malloc() and strcpy() you could also use strdup(), which both allocates memory and copies the string in one fell swoop

Reply With Quote
  #5  
Old June 20th, 2002, 08:54 AM
nucleuz nucleuz is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 8 nucleuz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks a lot for that!
When you're coding in PHP/Java all day, it is a bit of a head change to go to C
Everything is indeed declared in the syntax.h file, so I should be alright here.
This is my first serious C project ( just wait , it's actually a useful application!), so thanks for the help again!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > arrays and pointers


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway