• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

How to make a new char from a pointer


My question is how to make a new char from a pointer to work/change the char without having any effect on the pointer.

Example:
char *mychar = (char *)pointer[1];

In this case if I change anything in "mychar" then pointer[1] is being changed too.

Thank you for the help.

andme
0
andme
Asked:
andme
  • 5
1 Solution
 
gj62Commented:
Not sure what you are asking...

If you put in your code:

char *mychar = (char *)pointer[1];

and pointer[?] = char *

whether you change the contents of pointer[1], or the contents mychar, they will both change, since they both point to the same location in memory...

0
 
andmeAuthor Commented:
Hello gj62,

pointer [1] has a message.
I want to convert this message in lowercase without changing the original message in pointer[1].

Example:
 char *mychar = (char *)pointer[1];
 for (i = 0; i < strlen(mychar); i++)
             {
                mychar[i] = tolower(mychar[i]);
             }

As you said, if I change anything in mychar then pointer[1] will be changed also.
Is there a possibility to make an independent new char like in Java with valueOf(...).

andme


0
 
andmeAuthor Commented:
Hello gj62,

pointer [1] has a message.
I want to convert this message in lowercase without changing the original message in pointer[1].

Example:
 char *mychar = (char *)pointer[1];
 for (i = 0; i < strlen(mychar); i++)
             {
                mychar[i] = tolower(mychar[i]);
             }

As you said, if I change anything in mychar then pointer[1] will be changed also.
Is there a possibility to make an independent new char like in Java with valueOf(...).

andme


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
andmeAuthor Commented:
Hello gj62,

pointer [1] has a message.
I want to convert this message in lowercase without changing the original message in pointer[1].

Example:
 char *mychar = (char *)pointer[1];
 for (i = 0; i < strlen(mychar); i++)
             {
                mychar[i] = tolower(mychar[i]);
             }

As you said, if I change anything in mychar then pointer[1] will be changed also.
Is there a possibility to make an independent new char like in Java with valueOf(...).

andme


0
 
andmeAuthor Commented:
Hello gj62,

pointer [1] has a message.
I want to convert this message in lowercase without changing the original message in pointer[1].

Example:
 char *mychar = (char *)pointer[1];
 for (i = 0; i < strlen(mychar); i++)
             {
                mychar[i] = tolower(mychar[i]);
             }

As you said, if I change anything in mychar then pointer[1] will be changed also.
Is there a possibility to make an independent new char like in Java with valueOf(...).

andme


0
 
frogger1999Commented:
there isn't anything in C that is going to do that for you, but you can make a copy yourself using strcpy...
see the code below for an example of how to copy a string and make it lower case

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

void main()
{
unsigned int i=0;
char * lowerString;
char * dontChange = "dontChangeMe";

lowerString = (char *) malloc(strlen(dontChange)+1);
strcpy(lowerString, dontChange);

#if _WIN32 //_strlwr is windows only???
lowerString = _strlwr( lowerString);
#else //just have to make them lowercase manually
for(i = 0; i<strlen(lowerString); ++i)
{
    lowerString[i] = tolower(lowerString[i]);
}
#endif

printf("lowerString\n");

}
0
 
andmeAuthor Commented:

Thank you frogger1999,

that was helpful.

andme
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now