Jump to content

Using char as an argument for a variable that requires char*

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Macoder

Macoder

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
Hi

I have a function, StringEqual, that excepts two strings in char* format. I want to feed it two characters from the result of IthChar, wich returns in char format. Does anyone have any idea how to convert them? Or is there some way I could use a function like CharEqual()?

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Well, with char's, you can just do a direct equality check.
char first = IthChar(0);
char second = IthChar(1);

if (first == second)
{
    // Do stuff.
}

Wow I changed my sig!

#3
Macoder

Macoder

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
I can? Thanks! That's exactly what I needed. Just build my code and it works.

On a side note, my program ideas often die out because I don't know how to do proper unit conversion. Is there any resource on how to troubleshoot stuff like that?

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Have you been taught about Type Conversion? The Wikipedia article actually has a good overview of it, and it's int C/C++.

I hope that's what you meant.
Wow I changed my sig!

#5
krwq

krwq

    Newbie

  • Members
  • PipPip
  • 28 posts
I have no idea how function ithchar (maybe kind of char ithchar(char* s, int i) { return s[i]; }) works but hope this will help you:
char a = 'a';

char* pa = &a; // pointer (address) to a

char b = *pa; // value on the address pa


char arr[10] = "pointer";

char* p = arr; // when you put it on the output it will show the same as arr

p+=2;

printf("%s\n",p); // it will print "inter"

p++;

printf("%c\n",*p); // it will be 'n'

*p = 'X';

printf("%s\n",arr); // it will be poXnter

while (*p) p++;

*p = '2';

p[1] = 0; // equivalent to *(p+1)=0; null terminating

printf("%s\n",arr); // it will be poXnter2

2[arr] = 'Y'; // equivalent to *(2+arr)='Y';

printf("%s\n",arr); // it will be poYnter2b





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users