473,624 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char vs int

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *p;
p=malloc(4);
strcpy(p, "tja");
printf("%s\n", p);
free(p);
return 0;
}

Why is p a char and not an int? MPJ
Nov 14 '05 #1
28 2682


Merrill & Michele wrote:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *p;
p=malloc(4);
Check the return value of malloc. Always.
strcpy(p, "tja");
To be on the safe side, you could for example do
*p = '\0';
strncat(p,"tja" ,4-1);
printf("%s\n", p);
free(p);
return 0;
}

Why is p a char and not an int? MPJ


p is a char *. It is definitely not a char or int.
Please follow the simple rule of rereading your message
before you send it as we otherwise have to guess what
you could have meant apart from the on average high
level of weirdness by which many of your messages are
permeated.

My guess is:
"character constants are of type int and many functions
return int/have int parameters when we are dealing with
single characters -- why is it then that we store strings
in char arrays or memory pointed to by char * variables?"

Is that your question?
For that I have an answer.
-Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #2

"Merrill & Michele" <be********@com cast.net> wrote in message
news:yr******** ************@co mcast.com...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *p;
p=malloc(4);
strcpy(p, "tja");
printf("%s\n", p);
free(p);
return 0;
}

Why is p a char and not an int? MPJ


Becuase you are copying a char-array into it using a function which requires
a char*?
Nov 14 '05 #3
Merrill & Michele wrote:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *p;
p=malloc(4);
strcpy(p, "tja");
printf("%s\n", p);
free(p);
return 0;
}

Why is p a char and not an int? MPJ

Cause you, the programmer, made it a char pointer.
Which seems good, as you're coping a C string to it,
and strings are usually best manipulated as sequences
of chars, it is also what "%s" in your printf statement
would expect..
Nov 14 '05 #4
On Wed, 17 Nov 2004 08:40:20 -0600, Merrill & Michele wrote:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *p;
p=malloc(4);
strcpy(p, "tja");
printf("%s\n", p);
free(p);
return 0;
}

Why is p a char and not an int? MPJ


p is a pointer to char, not a char, and this is because that is how you
defined it, why in the world would you expect it to be an int??

Rob Gamble
Nov 14 '05 #5
Merrill & Michele wrote:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *p;
p=malloc(4);
strcpy(p, "tja");
printf("%s\n", p);
free(p);
return 0;
}

Why is p a char and not an int? MPJ


Are you asking why chars are stored in variables of type char?
And p is not a char.

Honestly, I usually try to give the questioner the benefit of the doubt
and I apply a good amount of charity of interpretation, but your
question does not make sense to me. I cannot even tell if you saying
that p is of type char is a mistake or ignorance on your part.

Please tell us why you think p could have been an int (or probably
pointer to int) instead of a pointer to char. Are you by any chance
referring to the fact that getchar and getc returns ints and not chars?
(So you see, I might be beginning to understand your question after all
though I didn't when I wrote the previous paragraph. But still I need
you to ask it.)

--
Thomas.
Nov 14 '05 #6
"Merrill & Michele" <be********@com cast.net> wrote:
<Snip>
int main(void){
char *p;
p=malloc(4);
strcpy(p, "tja"); <Snip>
Why is p a char and not an int? MPJ


I am going to assume that you are asking the following question.

Why do we store strings in arrays of char data types instead of arrays of
int data types?

Well I would answer that int data types while capable of holding the same
information as a char data type while doing so they are wasting X bytes of
space (size of X depends on the number of bytes in an int on the system
though on most 32 bit machines it is 4 bytes in length so there are 3 bytes
wasted.)

While this doesn't seem like much how about if you have a string buffer that
needs to store a 1024 characters. This would take up 1Kb of space using an
array of char data types but 4Kb of space (on most 32 bit machines again)
if it was using int data types.

If that is not your question then please repost with a clarified version and
I will have another go.
--------------
Jason Cooper
Nov 14 '05 #7

"J.L.Cooper " <A@A.COM> wrote in message
news:cn******** **@sun-cc204.lut.ac.uk ...
"Merrill & Michele" <be********@com cast.net> wrote:
<Snip>
int main(void){
char *p;
p=malloc(4);
strcpy(p, "tja"); <Snip>

Why is p a char and not an int? MPJ


I am going to assume that you are asking the following question.

Why do we store strings in arrays of char data types instead of arrays of
int data types?

Well I would answer that int data types while capable of holding the same
information as a char data type while doing so they are wasting X bytes of
space (size of X depends on the number of bytes in an int on the system
though on most 32 bit machines it is 4 bytes in length so there are 3

bytes wasted.)

While this doesn't seem like much how about if you have a string buffer that needs to store a 1024 characters. This would take up 1Kb of space using an array of char data types but 4Kb of space (on most 32 bit machines again)
if it was using int data types.

If that is not your question then please repost with a clarified version and I will have another go.
--------------
Jason Cooper

Thanks all for replies. That I thought I had partitioned the choice of data
type in this question with int and char shows exactly what I don't
understand. I'll hit K&R and the FAQs and get back to you. MPJ
Nov 14 '05 #8
In <30************ *@uni-berlin.de> Michael Mair <Mi**********@i nvalid.invalid> writes:


Merrill & Michele wrote:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *p;
p=malloc(4);


Check the return value of malloc. Always.


That's good advice for code that is meant to be executed. It's bad advice
for code that is meant to merely illustrate a point, because the error
checking code distracts the reader from the real purpose of the code.
strcpy(p, "tja");


To be on the safe side, you could for example do
*p = '\0';
strncat(p,"tja" ,4-1);


It's not any safer than the original, but it is less readable and has
higher maintenance overhead (the magical constant 4 appears in two places
instead of one).

Even in more complex examples, it is open to debate whether this is the
right thing to do: silently generating the *wrong* result is seldom a
better option than reporting an error. That's why strncat is seldom used
as a "safe" strcpy.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #9
> >[OP: Why is a char * a char and not an int]
Michael Mair: [insult snipped] My guess is:
"character constants are of type int and many functions
return int/have int parameters when we are dealing with
single characters -- why is it then that we store strings
in char arrays or memory pointed to by char * variables?"
Is that your question?
For that I have an answer I would imagine that your answer is similar to K&R §5.5 . But since I don't
get it, I would welcome the answer to that question.
Nils Selasdal:
Cause you, the programmer, made it a char pointer.
Which seems good, as you're cop[y]ing a C string to it,
and strings are usually best manipulated as sequences
of chars, it is also what "%s" in your printf statement
would expect..


K&R §2.2: "There are only a few basic data types in C: char, int, float,
double."
Is char * a data type? How about int **? MPJ


Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2200
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my attempt at it, but it seems to be lacking... I'm aware that strdup() is nonstandard (and a bad idea for C++ code) - please just bear with me: /* Assume relevant headers are included */ class char_ptr {
5
9732
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an array of char > > typedef struct { > > char name; > > int age; > > int id; > > } person; > >
5
2528
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now when my function returns, the values stored in the char* are some garbage values (perhaps because I didn't allocate any memory for them).. but even if I allocate memory in the function, on the return of this function I see garbage.. here is my...
2
3402
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has raised a doubt in my mind on the same issue. Either through ignorance or incompetence, I've been unable to resolve some issues. 6.4.4.4p6 states...
5
3957
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
12
10077
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display it. here is the 4 errors. c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from 'char ' to 'char '
18
4044
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
3216
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
6778
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
9955
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
8236
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8173
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8679
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8475
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2606
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.