C++ question. (pointers)

Anything and Everything about Uplink

Moderators: jelco, bert_the_turtle, Chris, Icepick, Rkiver

SoftICE
level1
level1
Posts: 32
Joined: Fri Jul 01, 2005 11:07 am
Location: Israel

C++ question. (pointers)

Postby SoftICE » Tue Jul 19, 2005 6:10 pm

Hay.
I want to ask a question.
this topic is for c++ programmers.


Ahmm. I want to create an array of chars, string, and put a value in it later, but I still don't know what's the lenght of the value.
So, Iv'e been told that I can do that by using pointers,
So can someone give me a clue how can I do it?

A piece of code will be very very helpful!

thank you,
Myself. :)
Asleep in your arms I'm drifting
I'm falling in sorrow
Dead to the world you left me
In footsteps I follow
Stewsburntmonkey
level5
level5
Posts: 11553
Joined: Wed Jul 10, 2002 7:44 pm
Location: Nashville, TN
Contact:

Postby Stewsburntmonkey » Tue Jul 19, 2005 6:22 pm

Well it would be easiest to use a string library. <string> is the standard one.

so something like:

Code: Select all

#include <iostream>
#include <string>

int main() {
  string str1("Hello");
  string str2;
  string str3;

  cin >> str2;
  str3 = str1 + str2;

  return 0;
}



str1 would have a value "Hello", str2 would be whatever you entered, and str3 would be the two strings concatenated.

Alternatively you could create a pointer to a character array. So a function that makes a copy of a string (null terminated char array) and does something with it could look like this:

Code: Select all

#include <stdlib.h>

void foo(char text[])
{
  int num_chars;
  char *copy;

  num_chars = strlen(text) + 1;

  /* Allocate room for the string. */
  copy = (char *)malloc(sizeof(char) * num_chars);

  /* Copy the contents of the string. */
  strcpy(copy, text);

  ...

  /* Use the copy. */

  copy[0] = 'A';

  ...

  /*
   * Don't waste memory.  We are done with the
   * dynamically-allocated string, so free it.
   */
  free(copy);
}

SoftICE
level1
level1
Posts: 32
Joined: Fri Jul 01, 2005 11:07 am
Location: Israel

Postby SoftICE » Tue Jul 19, 2005 7:00 pm

Thanks!
Asleep in your arms I'm drifting

I'm falling in sorrow

Dead to the world you left me

In footsteps I follow
jolo78957
level2
level2
Posts: 80
Joined: Thu Sep 30, 2004 9:58 pm

Postby jolo78957 » Fri Jul 22, 2005 5:13 pm

Shouldn't this be on the DEVCD board?
Stewsburntmonkey
level5
level5
Posts: 11553
Joined: Wed Jul 10, 2002 7:44 pm
Location: Nashville, TN
Contact:

Postby Stewsburntmonkey » Fri Jul 22, 2005 5:15 pm

Seeing as this is not related to the DevCD and seeing as the DevCD board is only accessible if you own the DevCD, the DevCD forum would be a poor place for this. :)
exosyphen
level2
level2
Posts: 240
Joined: Wed Jul 02, 2003 8:14 am
Location: Romania
Contact:

Postby exosyphen » Fri Jul 22, 2005 7:07 pm

Or :

char *unknownSizeString;

unknownSizeString = new char[strlen(stringWeWantToStore)];
strcpy(unknownSizeString, stringWeWantToStore);


While at it, I recommend reading the Quake sourcecode. The best C code you can find around, and you can learn some amazing tricks with pointers :)

Return to “General”

Who is online

Users browsing this forum: No registered users and 7 guests