pointer problems

Post questions regarding programming in C/C++ in Linux/Unix.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

pointer problems

Postby walterw » Sun Dec 28, 2003 10:13 pm

Hi,

I am trying to figure out pointers. Whenever I run this, it prints my gcc version instead of the string I give it. Weird...

Walter

#include <strings.h>
#include <string.h>
#include <string>
#include <iostream>

char* toLOWER(char *input);
using namespace std;

main()
{
//string a("testing");
char *a = "TESTING";
cout << a << "\tThat was string a.\n";
cout << toLOWER(a) << "\tThat was the lowercase version of string a.\n";

return(0);
}

char* toLOWER(char *input)
{
char *lowerCASE = "";
for(int i = 0; i < strlen(input); i++)
{
lowerCASE += tolower(input[i]);
cout << input[i] << "\tThe character at:\t" << i << "\n";
cout << lowerCASE << "\tThat was lowerCASE:" << i << "\n";
}
return(lowerCASE);
}
walterw
 
Posts: 22
Joined: Fri Dec 26, 2003 8:18 am

Postby MXP » Wed Dec 31, 2003 4:21 am

lowerCASE is a memory address. When you add to it in this line

lowerCASE += tolower(input[i]);

you are advancing lowerCASE by tolower(input[i]) number of chars not appending the lower case character of input[i].
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

pointer problems

Postby walterw » Wed Dec 31, 2003 4:00 pm

Thanks very much, I am new to C++ and these pointer things aren't working out so well for me. This is what I did to fix it, a work-around...


Walter

char* toLowerCase(char input[])
{
int length = strlen(input);
for(int i = 0; i < length; i++)
{
input[i] = (char)tolower(input[i]);
cout << input[i] << "\n";
}
char *output = input;
return(output);
}
walterw
 
Posts: 22
Joined: Fri Dec 26, 2003 8:18 am


Return to Unix/Linux

Who is online

Users browsing this forum: Google [Bot] and 1 guest