Gå til innhold

Opprette array med setninger.


Anbefalte innlegg

Eg er ganske grønn i c/c++ , og sitter og prøver å få opprette et array som inneholder flere setninger.

 

Noe slikt som dette:

Beskrivelser[0] = "Beskrivelse 1";

Beskrivelser[1] = "Beskrivelse 2";

Beskrivelser[2] = "Beskrivelse 3";

osv.

 

Følgende er det eg har fått til å fungere så langt:

char Beskrivelser[][255] =
{
{ "Beskrivelse 1" },
{ "Beskrivelse 2" },
{ "Beskrivelse 3" },
};

 

Her får eg i det minste lagret setningene i arrayet, men sliter med å få hentet ut igjen hele setningen.

Endret av _TT_
Lenke til kommentar
Videoannonse
Annonse

Takker for svar, fikk dette til å fungere nå. Men eg kom opp med en ny problemstilling;

Ville det vært mulig å sette opp et array som inneholdt flere setninger på samme array indexen?

 

Eksempel:

Ting[1][1] = "Navn 1"

Ting[1][2] = "Beskrivelse 1"

Ting[1][3] = "Egenskaper 1"

Ting[2][1] = "Navn 2"

Ting[2][2] = "Beskrivelse 2"

Ting[2][3] = "Egenskaper 2"

osv.

 

Det skal vel i teorien gå? Men eg finner ikke ut av hvordan eg skal definere arrayet...

Endret av _TT_
Lenke til kommentar

som Klette sier er det nå greit å gå over til C++, men ønsker du å gjøre dette i C blir det slik:

 

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

int main(){
 typedef char* cstr;
   
 // Each string can have a maximum size of 100 characters.
 int str_size = 100;

 int x_axis = 10; int y_axis = 10;

 // 2 dimensional dynamic array of strings.
 cstr** strings;

 // Allocate for Y-axis.
 strings = (cstr**)malloc(y_axis * sizeof(cstr**));

 // Allocate for X-axis.
 {int y;
   for(y = 0; y < y_axis; y++)
     strings[y] = (cstr*)malloc(x_axis * sizeof(cstr*));}

 // Allocate space for strings (finally).
 {int y, x;
   for(y = 0; y < y_axis; y++)
     for(x = 0; x < x_axis; x++)
       strings[y][x] =(cstr) malloc(str_size * sizeof(char));}

 // Fill the array with data (the strings).
 {int y, x;
   for(y = 0; y < y_axis; y++)
     for(x = 0; x < x_axis; x++)
       snprintf(strings[y][x], str_size, "Hello World from array-position (%i,%i)!", x, y);}
   
 // Print the data out.
 {int y, x;
   for(y = 0; y < y_axis; y++)
     for(x = 0; x < x_axis; x++)
       printf("%s\n", strings[y][x]);}

 // Now we got to remember to clean up after us.
 // We do this "in reverse order" of how we allocate. First the strings..
 {int y, x;
   for(y = 0; y < y_axis; y++)
     for(x = 0; x < x_axis; x++)
       free(strings[y][x]);}

 // ..then the X-axis..
 {int y;
   for(y = 0; y < y_axis; y++)
     free(strings[y]);}

 // ..and finally the outher Y-axis.
 free(strings);

 return 0;}

 

lars@ibmr52:~/programming/c$ gcc -W -g blah.c -o blah && ./blah

Hello World from array-position (0,0)!

Hello World from array-position (1,0)!

Hello World from array-position (2,0)!

Hello World from array-position (3,0)!

Hello World from array-position (4,0)!

Hello World from array-position (5,0)!

Hello World from array-position (6,0)!

Hello World from array-position (7,0)!

Hello World from array-position (8,0)!

Hello World from array-position (9,0)!

Hello World from array-position (0,1)!

Hello World from array-position (1,1)!

Hello World from array-position (2,1)!

Hello World from array-position (3,1)!

Hello World from array-position (4,1)!

Hello World from array-position (5,1)!

Hello World from array-position (6,1)!

Hello World from array-position (7,1)!

Hello World from array-position (8,1)!

Hello World from array-position (9,1)!

Hello World from array-position (0,2)!

Hello World from array-position (1,2)!

Hello World from array-position (2,2)!

Hello World from array-position (3,2)!

Hello World from array-position (4,2)!

Hello World from array-position (5,2)!

Hello World from array-position (6,2)!

Hello World from array-position (7,2)!

Hello World from array-position (8,2)!

Hello World from array-position (9,2)!

Hello World from array-position (0,3)!

Hello World from array-position (1,3)!

Hello World from array-position (2,3)!

Hello World from array-position (3,3)!

Hello World from array-position (4,3)!

Hello World from array-position (5,3)!

Hello World from array-position (6,3)!

Hello World from array-position (7,3)!

Hello World from array-position (8,3)!

Hello World from array-position (9,3)!

Hello World from array-position (0,4)!

Hello World from array-position (1,4)!

Hello World from array-position (2,4)!

Hello World from array-position (3,4)!

Hello World from array-position (4,4)!

Hello World from array-position (5,4)!

Hello World from array-position (6,4)!

Hello World from array-position (7,4)!

Hello World from array-position (8,4)!

Hello World from array-position (9,4)!

Hello World from array-position (0,5)!

Hello World from array-position (1,5)!

Hello World from array-position (2,5)!

Hello World from array-position (3,5)!

Hello World from array-position (4,5)!

Hello World from array-position (5,5)!

Hello World from array-position (6,5)!

Hello World from array-position (7,5)!

Hello World from array-position (8,5)!

Hello World from array-position (9,5)!

Hello World from array-position (0,6)!

Hello World from array-position (1,6)!

Hello World from array-position (2,6)!

Hello World from array-position (3,6)!

Hello World from array-position (4,6)!

Hello World from array-position (5,6)!

Hello World from array-position (6,6)!

Hello World from array-position (7,6)!

Hello World from array-position (8,6)!

Hello World from array-position (9,6)!

Hello World from array-position (0,7)!

Hello World from array-position (1,7)!

Hello World from array-position (2,7)!

Hello World from array-position (3,7)!

Hello World from array-position (4,7)!

Hello World from array-position (5,7)!

Hello World from array-position (6,7)!

Hello World from array-position (7,7)!

Hello World from array-position (8,7)!

Hello World from array-position (9,7)!

Hello World from array-position (0,8)!

Hello World from array-position (1,8)!

Hello World from array-position (2,8)!

Hello World from array-position (3,8)!

Hello World from array-position (4,8)!

Hello World from array-position (5,8)!

Hello World from array-position (6,8)!

Hello World from array-position (7,8)!

Hello World from array-position (8,8)!

Hello World from array-position (9,8)!

Hello World from array-position (0,9)!

Hello World from array-position (1,9)!

Hello World from array-position (2,9)!

Hello World from array-position (3,9)!

Hello World from array-position (4,9)!

Hello World from array-position (5,9)!

Hello World from array-position (6,9)!

Hello World from array-position (7,9)!

Hello World from array-position (8,9)!

Hello World from array-position (9,9)!

lars@ibmr52:~/programming/c$

 

edit:

om du ønsker et statisk array, blir det noe slik:

  typedef char* cstr;

 cstr static_strings[2][3] = {{ "a", "b", "c"},
                              { "d", "e", "f"}};
 
 {int y, x;
   for(y = 0; y < 2; y++)
     for(x = 0; x < 3; x++)
       printf("%s\n", static_strings[y][x]);}

 

edit2:

jeg greiede å glømme at C er "row-major", så man må si array[y][x] .. rettet på dette nå .. det er mulig å snu på dette for dynamiske arrayer, men jeg orker ikke å gå inn på dette nå

Endret av lnostdal
Lenke til kommentar
  • 2 uker senere...

Hvis vi snakker om statiske strenger er vel det aller enkleste:

 

char *values[] =

{

"One",

"Two",

"Three",

};

 

som du kan iterere på følgende måte:

 

for (int i = 0; i < (sizeof(values)/sizeof(char *)); i++)

printf("%s\n", values);

 

En god blanding av C og C++, men man bruker da det beste av to verdener? :)

 

Smeg out...

Lenke til kommentar

Grøss, hvorfor anbefaler dere bruk av char-pointere i C++?

 

Bruk vector + string. Hvis du i tillegg vil definere vectorene dine lett, bruk boost's assign-bibliotek (http://www.boost.org). Dette gir en god operator+= for blant annet vector-klassen.

 

Eksempel:

 

#include <iostream>

#include <string>

#include <boost/assign/std/vector.hpp>

 

using namespace std;

using namespace boost::assign;

 

int main()

{

vector<string> v;

v += "hei", "på", "deg";

cout << v.size() << endl; // burde gi '3' som output

return 0;

}

Lenke til kommentar
  • 2 uker senere...

Om man bruker glibmm bruker man jo C++, ser du ikke forskjellen? Man _kan_ også gjøre disse tingene v.h.a. C på litt enklere måter enn å bruke _kun_ det som følger med C; det finnes biblioteker for containers o.l. til C også.

 

..men..

 

Hadde man i utgangspunktet brukt C++ hadde man allerede hatt STL, og det ville mest sansynlig allerede inneholt hva du er ute etter uansett.

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...