?
Solved

How to make a new char from a pointer

Posted on 2003-03-21
7
Medium Priority
?
221 Views
Last Modified: 2010-04-15

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
Comment
Question by:andme
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8185450
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
 

Author Comment

by:andme
ID: 8185481
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
 

Author Comment

by:andme
ID: 8185504
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:andme
ID: 8185515
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
 

Author Comment

by:andme
ID: 8185525
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
 
LVL 1

Accepted Solution

by:
frogger1999 earned 300 total points
ID: 8185678
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
 

Author Comment

by:andme
ID: 8185757

Thank you frogger1999,

that was helpful.

andme
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question