---
title: "textsträng i array"
type: "forum-thread"
url: "https://www.webforum.nu/amne/c-cpp/102315-textsträng-i-array"
topic: "C/C++"
topic_url: "https://www.webforum.nu/amne/c-cpp"
author: "tosses"
published: "2004-04-25T18:43:10.000Z"
updated: "2004-04-25T19:17:15.000Z"
replies: 1
views: 147
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/c-cpp/102315-textsträng-i-array"
---

# textsträng i array

## #1 — tosses, 2004-04-25T18:43Z

Finns det något sätt att spara textsträngar i en array utan att använda \<cstring\> ??

Ex:

str\[1\]="hejsan";
str\[2\]="Kalle kula";

osv..

Permalänk: https://www.webforum.nu/p/102315

## #2 — Viktor, 2004-04-25T19:17Z

Då får du göra en array med char pekare

C++ kod

```
#include <iostream>
#include <cstring> //Behövs för strcpy funktionen.

using namespace std;

int main(int arc, char *argv[])
{
  char *strArray[2];
  strArray[0]=new char[100];
  strcpy(strArray[0],"hejsan");
  strArray[1]=new char[100];
  strcpy(strArray[1],"kalle kula");
  cout << strArray[0] << endl;
  cout << strArray[1] << endl;
}
```

C kod

```
#include <stdio.h>

int main(int arc, char *argv[])
{
  char *strArray[2];
  strArray[0]=(char*)malloc(sizeof(char)*100);
  strcpy(strArray[0],"hejsan");
  strArray[1]=(char*)malloc(sizeof(char)*100);
  strcpy(strArray[1],"kalle kula");
  printf("%s\n%s\n",strArray[0],strArray[1]);
}
```

/Viktor

Permalänk: https://www.webforum.nu/p/1330551

---

Tråden på webben: https://www.webforum.nu/amne/c-cpp/102315-textsträng-i-array
