Author Topic: [C] Making a substr function  (Read 5840 times)

0 Members and 1 Guest are viewing this topic.

Offline Zeb

  • C= 64
  • **
  • Posts: 40
  • Karma: 4
    • View Profile
    • Amiga Software Downloads
[C] Making a substr function
« on: September 20, 2008 »
Since C has no feature to extract part of a string from a string (like PHP's substr does) I thought I'd modify an existing routine...

Code: [Select]
int substr(char **tFrom,int tTo,int tOffset, int tLen) {
  tTo = (char*) malloc(strlen(tFrom));
  strncpy(tTo, tFrom+2, 5);
  return tTo;
}

Any idea why this doesn't work?
Code: [Select]
  tTo = (char*) malloc(strlen(tFrom));
                                     ^
clparse.c(47) : Error: need explicit cast to convert
from: char *
to  : int
  strncpy(tTo, tFrom+2, 5);
                         ^
clparse.c(48) : Error: need explicit cast for function parameter 1 to get
from: int
to  : char *
--- errorlevel 1
if (empty($coffee)) {new $coffee();} else {drinkCoffee('fast');}

Amiga coder (assembler, E), PC coder (learning C)

PC SPECS:
W7 x64 Ultimate, AMD Athlon X2 Dual Core 5200 @ 2.6GHz, 4GB DDR2 PC6400 (800) RAM, JeanTech Storm 700W PSU, ATI Radeon X1650 Pro 256MB PCI-Ex16, SoundBlaster Audigy Platinum 2 eX, 3TB hard drive storage, Dual 22"WS TFT, Epson R300 printer, Epson V100 Perfection scanner, DL-DVD-+RAM/ROM/RW, DL-DVD-+RW, X7 laser gaming mouse and more...

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: [C] Making a substr function
« Reply #1 on: September 20, 2008 »
Code: [Select]
tTo = (char*) malloc(strlen(tFrom));here the input parameter "tTo" is an "int" but you assign a char* to it.

How about this?
Code: [Select]
char* substr(char *src, int start, int length)
{
  char *dst;
  if (length==0)
    length= strlen(src+start);
  dst = (char*) malloc(length+1);
  memcpy(dst, src+start, length);
  dst[length]=0;
  return dst;
}
Challenge Trophies Won:

Offline Zeb

  • C= 64
  • **
  • Posts: 40
  • Karma: 4
    • View Profile
    • Amiga Software Downloads
Re: [C] Making a substr function
« Reply #2 on: September 20, 2008 »
Thanks, that works!

How do you give karma?  Can't find the control!

This is where I get mega confused with C - where to use pointers/references/direct access to variables :/
if (empty($coffee)) {new $coffee();} else {drinkCoffee('fast');}

Amiga coder (assembler, E), PC coder (learning C)

PC SPECS:
W7 x64 Ultimate, AMD Athlon X2 Dual Core 5200 @ 2.6GHz, 4GB DDR2 PC6400 (800) RAM, JeanTech Storm 700W PSU, ATI Radeon X1650 Pro 256MB PCI-Ex16, SoundBlaster Audigy Platinum 2 eX, 3TB hard drive storage, Dual 22"WS TFT, Epson R300 printer, Epson V100 Perfection scanner, DL-DVD-+RAM/ROM/RW, DL-DVD-+RW, X7 laser gaming mouse and more...

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C] Making a substr function
« Reply #3 on: September 21, 2008 »
You can't give out Karma until you reach 20 or 25 posts :)

Once you do get to grips with pointers you can see why C and C++ are amazing languages!

Jim

Challenge Trophies Won:

Offline Zeb

  • C= 64
  • **
  • Posts: 40
  • Karma: 4
    • View Profile
    • Amiga Software Downloads
Re: [C] Making a substr function
« Reply #4 on: September 21, 2008 »
The funny thing is, I already know what pointers are having coded assembler for about 10 years!

What I always found confusing is where to pass a pointer and when to pass the variable! I find C so confusing!
if (empty($coffee)) {new $coffee();} else {drinkCoffee('fast');}

Amiga coder (assembler, E), PC coder (learning C)

PC SPECS:
W7 x64 Ultimate, AMD Athlon X2 Dual Core 5200 @ 2.6GHz, 4GB DDR2 PC6400 (800) RAM, JeanTech Storm 700W PSU, ATI Radeon X1650 Pro 256MB PCI-Ex16, SoundBlaster Audigy Platinum 2 eX, 3TB hard drive storage, Dual 22"WS TFT, Epson R300 printer, Epson V100 Perfection scanner, DL-DVD-+RAM/ROM/RW, DL-DVD-+RW, X7 laser gaming mouse and more...

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: [C] Making a substr function
« Reply #5 on: September 21, 2008 »
Quote
having coded assembler
you'll notice that C is in many cases very near to assembly.
today there's not much reason to use only C (except of some embedded platforms).
In C++ you'll find that alot of these kind of things are already done.
Have a look at std::string for example...
Challenge Trophies Won:

Offline Zeb

  • C= 64
  • **
  • Posts: 40
  • Karma: 4
    • View Profile
    • Amiga Software Downloads
Re: [C] Making a substr function
« Reply #6 on: September 21, 2008 »
Thanks!

This book has [very] loosely covered variables and types, now I'm onto expressions and statements - can't wait until I hit the strings section as that's been my main cause for concern.

With assembler you can "see" exactly how a string is being handled/manipulated (especially with the help of MonAm) but C for me has been a little bit on the HELP I CAN'T FIGURE THIS OUT! side :D
if (empty($coffee)) {new $coffee();} else {drinkCoffee('fast');}

Amiga coder (assembler, E), PC coder (learning C)

PC SPECS:
W7 x64 Ultimate, AMD Athlon X2 Dual Core 5200 @ 2.6GHz, 4GB DDR2 PC6400 (800) RAM, JeanTech Storm 700W PSU, ATI Radeon X1650 Pro 256MB PCI-Ex16, SoundBlaster Audigy Platinum 2 eX, 3TB hard drive storage, Dual 22"WS TFT, Epson R300 printer, Epson V100 Perfection scanner, DL-DVD-+RAM/ROM/RW, DL-DVD-+RW, X7 laser gaming mouse and more...