d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Array Of Pointers To Strings?
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Feb 27 2013 10:32am
Say I wanted to have a 2d array in C

First array contains a bunch of random numbers that have to point to specific strings based off of what number they are like:

32 -> taco
21-> fish
99-> cheese
ect..

How would I go about doing this?

Like:

char **stringset;

Not sure how to exactly initialize the 2d array.

The numbers are quite spread out, they jump from like 32 to 60 then to 72

Would I have to make a pointer pointing to an int pointing to a char array?

This post was edited by lopelurag on Feb 27 2013 10:38am
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Feb 27 2013 01:00pm
a 2d array is an array of arrays. for example, here's a 3x3 array of integers:

Code
[[1,2,3][4,5,6][7,8,9]]


which conceptually looks like this:
Code
1 2 3
4 5 6
7 8 9


in C, a 2d array would be implemented as an array of pointers (which, in turn, each point to array)

This post was edited by irimi on Feb 27 2013 01:04pm
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Feb 27 2013 03:30pm
Ok what I'm having problem with atm is implicit function calls. Keep getting an error on compilation saying I have implicit function calls.

Example of problem...

Code
#include <stdio.h>

int main(){
int = 1;

print_function(1);

}

int print_function(int number){

int new_numberr = multiply_number(number);
printf(new_number);

return 1;
}

int multiply number(int num){

does something
}


It'll say that multiply number is declared implicitly?

This post was edited by lopelurag on Feb 27 2013 03:30pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 27 2013 04:33pm
missing an underscore
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 27 2013 04:55pm
Quote (carteblanche @ Feb 27 2013 06:33pm)
missing an underscore


also missing function prototypes
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Feb 27 2013 05:06pm
Yeah those errors have nothing to do with 2d arrays at all
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Feb 27 2013 05:11pm
What you're looking for is an associative array and there are no default implementations of those in C. In C++, you have std::map and std::unordered_map, in C you have to write one on your own.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 27 2013 06:42pm
Quote (KrzaQ2 @ Feb 27 2013 07:11pm)
What you're looking for is an associative array and there are no default implementations of those in C. In C++, you have std::map and std::unordered_map, in C you have to write one on your own.


this.

a fairly easy way to do this is by using structs imo

Code
struct something{
  int num;
  char name[56];
};

int main()
{
struct someting mystruct[10];

for(int i = 0; i < 10; i++)
{
  mystruct[i].num = rand()%100+1;
  mystruct[i].name = rand()%26+'a';
}

return 0;
}


another way to do this would to have two arrays

one an array of integers and another array of characters then line up the elements so that element n in the intereger array relates to element n in the character array.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll