Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Why use an asterisk after a type?

Parent

Why use an asterisk after a type?

+3
−0
#include<stdio.h>
struct Node{
     int data;
     struct Node* next;
};

Here I used an asterisk after Node. What is it used for? What if I don't put any asterisk after Node (both Node's are structures)?

Is *ptr and ptr* same?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Pointers (3 comments)
Post
+3
−0

The * for variables and not mathematical operators are the pointers.

Assigning a pointer goes this way:

char *text; // string

Here, we assign a pointer named text and its type is a char, but alternatively, this is a string.

As for your question, int *ptr is basically the same as int* ptr, only being a pointer of an int, and nothing else. Your given struct Node* next is the same as struct Node *next, and I don't think ptr* even exists, unless it's made as some union or struct. struct *Node wouldn't work likely.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Which language did you use? (5 comments)
Which language did you use?
deleted user wrote almost 3 years ago

Usually, this question isn't necessary. But, you wrote char *text; //string Character is character how it can be string? In C, string is array of characters. So, it should be char *text[10], shouldn't it? In C++, we can directly use string type.

General Sebast1an‭ wrote almost 3 years ago · edited almost 3 years ago

I use C99, a version of C. char * in itself isn't a string but somewhat acts like one when something is initialized in it and maybe even inputted. string is not a thing in C.

Skipping 1 deleted comment.

hkotsubo‭ wrote almost 3 years ago

@Istiak‭ Actually, it's a little bit more complicated than that. A pointer to some type T is, let's say, "interchangeable" with an array of T (actually, the pointer points to the array's first element). In C, a string is actually an array of chars (terminated by \0), so a char * (a pointer to a char) can be used as if it were an array of char (thus, both are also called "string", although there's no such thing as a string type in C)

hkotsubo‭ wrote almost 3 years ago · edited almost 3 years ago

If we're meant to be pedantic/technically accurate, we'd never call it "string", but if you call it that way, people - at least anyone familiar with the language - will usually know that you actually mean "a contiguous sequence of chars terminated by the null character (\0)" (regardless of how it's being represented - either by an array of char or by a char *). Maybe this could help you to understand better: http://c-faq.com/aryptr/aryptr2.html

elgonzo‭ wrote almost 3 years ago · edited almost 3 years ago

hkotsubo‭, the libc functions dealing with char*/char[] are called strcmp, strlen, etc... Note the "str". The related header file is called string.h. The documentation regarding these functions talks about strings. I don't think we need to be pedantic here. The emphasis should rather be on not confusing char*/char[]-based strings with other string types (such as C++ std::string, or MS' CString, for example; which of course are also sequences of characters).