function pointer C programming [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm not sure what the following does and i'm hoping someone can clarify the purpose of having the asterisk in front of the functions name:
char *Foo(char *ptr) {
return NULL;
}
I understand that you can pass by value the memory location of something in the function argument call and *ptr would be the pointer to it. I understand you can create a pointer function that can be used to point to other functions like a regular pointer points to variable memory location but in this case this is not a function pointer that we can point to other functions, or is it? This seems like a real function.

Foo is a function.
It has input: ptr of type char*
It has output of type char*
char* means "pointer to char"
it returns NULL.
That is the most plain explanation I can think of.

its misleading you, the * by the name isn't related to the name
it means the same as char* Foo(char* ptr)
which means a function which takes a char* and returns a char*

Related

What does &struct_name and sometype ** variable_name do exactly? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I come from a Java background and I started reading K&R, but the progress is extremely slow, because I know most of it, but still have to read everything again. So, I was thinking that I could maybe ask here some things about the C programming language to learn things a lot faster.
What I want to know is
What happens when I pass a struct variable with a &-prefix as an argument to a function? The code sample that I am trying to understand is:
struct somestruct st;
somefunction(&st);
1.1. What kind of signature does somefunction need to have and what exactly is passed?
1.2. A pointer to the struct variable would be *st instead, right?
What does it mean when a function has as a parameter sometype ** variable_name? The code that I want to understand is:
int main(int argc, char **argv)
The whole code that I want to understand is here: https://stackoverflow.com/a/35355069/3668527
Please no explanations of the code. I know what it does. I just need to know what those strange new C operators & and ** mean.
Edit: Oh, and please tell me how these operators are called!
& get pointer to that variable.
The function signature should be: void somefunction(struct somestruct *st), i.e. it will accept pointer to that structure.
strct * means pointer to strct, strct ** means pointer to pointer to strct etc.
What happens when I pass a struct variable with a &-prefix as an argument to a
function? The code sample that I am trying to understand is:
The & operator returns the adress of an object.
What kind of signature does somefunction need to have and what exactly is passed?
void somefunction(struct somestruct *pointer);
You can put in "const" in a few places to tell the compiler that you don't want to allow the method to do any changes.
1.2 A pointer to the struct variable would be *st instead, right?
Depends in which context "*st" is used, if you just want to create a pointer do it that way:
somestruct *pointer = NULL; // or init it somehow
Edit: Oh, and please tell me how these operators are called! Thanks!
They are called adress operators.
What does it mean when a function has as a parameter sometype ** variable_name
That means that the parameter is a pointer pointing to another pointer.

Getting error when trying to malloc memory to pointer variable declared separately [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have declared a char pointer in the following manner:
School *student[10];
for(i=0;i<10;i++){
*student[i] = malloc(sizeof(Student)); <--- Error points here
}
The error I get is:
incompatible types when assigning to type 'struct Student' from type 'void*'
Does anyone know why I am getting this error?
But how come if I were to allocate memory in the same line it would be with the star. For example: Student *name = malloc(sizeof(Student)); Why does this work? Im a bit confused
*student[i] = malloc(sizeof(School)); should be student[i] = malloc(sizeof(School));
students is an array of pointer to struct of type School. So you need to allocate for each pointer in that array. When you write *student[i] - you are dereferencing pointer i instead of allocating memory for it.
And as NicolasMiari pointed out, the sizeof operator must apply to School instead of student.
But how come if I were to allocate memory in the same line it would be with the star. For example: Student *name = malloc(sizeof(Student)); Why does this work? Im a bit confused
That's different. When you write Student *name = malloc(sizeof(Student)); you are both declaring a pointer and initialize it with malloc. You can do both steps in a single line like that. Alternatively, you declare it first, then assign it with malloc in a different line - in that case you must remove the asterisk.
You may want to refer to this question pointer initialization and pointer assignment.

strcpy() with copying string from one structure to another [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
it seems that this is a duplicate question, but I searched stackoverflow's question about that point and non is like to my problem(I think)
I've two variables of a struct each has its own pointer to char, when I tried to copy from one variable's string to another variable's string, nothing happened, although no errors appear, just warning
implicit declaration of function strcpy
incompatible implicit declaration of built-in function 'strcpy'
I read from some questions on stackoverflow that you'd better to use strdup() function instead of strcpy() but when I did, I had an error
too many arguments to function 'strdup'
I read that there's a problem with strcpy() called "segmentation fault" and I knew it's about memory allocation, I don't totally understand what it's exactly and don't know if it's the problem with my code?
and this is my code
struct p
{
char *name;
struct p *next;
};
struct p *ptrary[10];
int main(void)
{
struct p p,q;
p.name="xyz";
p.next=NULL;
ptrary[0]=&p;
strdup(q.name,p.name);
ptrary[1]=&q;
printf("%s\n",ptrary[1]->name);
return 0;
}
so what is the problem and how I can solve it?
strdup() takes only one argument; it malloc's and returns a new block of heap memory containing the duplicated string. (See https://stackoverflow.com/questions/252782/strdup-what-does-it-do-in-c)
Which probably also points to the problem you were having before -- were you remembering to malloc the space for q to copy p's contents into?
change
strdup(q.name,p.name);
to
q.name = strdup(p.name);
see man page of strdup for further details. The strdup() function returns a pointer to a new string.
full code:
#include <stdio.h>
#include <string.h>
struct p
{
char *name;
struct p *next;
};
struct p *ptrary[10];
int main(void)
{
struct p p,q;
p.name="xyz";
p.next=NULL;
ptrary[0]=&p;
q.name = strdup(p.name);
ptrary[1]=&q;
printf("%s\n",ptrary[1]->name);
return 0;
}

typecasting a pointer to a multidimensional array in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
# include <stdio.h>
# include <stdlib.h>
int main(int argc, char *argv[])
{
int a[5][10][2];
int *p;
p = (int(*)[10][2])p;//Gives error!
return EXIT_SUCCESS;
}
I want to type cast p to type so that it can act as a pointer to the given 3-d array?Is there a way to do so.I am applyindg the idea that the type of a variable is everything except variable name.
Why are you trying to "typecast" anything? Why would you expect a value "typecasted" to (int(*)[10][2]) to be compatible with an int * pointer? And why does your original code assigns p to p, completely ignoring a?
This is what you can do
int a[5][10][2];
int (*p)[10][2] = a;
Now p is a pointer that can be used to access a, i.e. p[i][j][k] is equivalent to a[i][j][k]. No typecasting necessary.
If you write p = (int(*)[10][2])a; it won't give you any errors, may be a warning. You are thinking that p will be converted to pointer to a 3-D array, which is wrong. Try this statement after assigning a to p.
printf("addresses %u %u",p,p+1);
According to you, output should be something similar to this(lets say) "addresses 9990000 99940000", because you are thinking p is pointing to 3-D array. However you will get similar to "addresses 9990000 9990004", proving that p is a pointer to an integer.

Casting int to char* [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a little problem with casting int to char* (string)... is it even possible in C?
I'll try to explain why i need this.
I can cast int to char but I need cast int to char*.
I had a int varriable (int number_of_revisions)
and I need convert this number of revisions to char * becouse I need create a name of file and the number of revision is part of the name.... so there is part of code for better imagination of this problem.
int number_of_revision = 970; // 970 just for example
char * version;
char * new_name;
char ch_number_of_rev[4];
version = "0.";
itoa(number_of_revision,ch_number_of_rev,10);
//strcat(version, ch_num_o_rev ); // doesn't work becouse ch_number_of_rev is char and strcat requires char*
please I need quick help... Have anybody any idea how to do it? ...
but I need cast int to char*
Casting only changes the type - it does not change the value within the variable. If you need to convert an int to array of chars (i.e. a string) then use sprintf or snprintf:
char* buffer = ... allocate a buffer ...
int value = 970;
sprintf(buffer, "%d", value);
Converting int to string in c
Also, you have not allocated any memory for version - use malloc and allocate some memory.
strcat here won't work because you haven't allocated any space to store the result in. Your version is probably in read-only memory anyway, so you'd get a segfault, otherwise you'll get memory corruption. So make sure to allocate enough space for it, e.g. by using
char version[10] = "0.";
You may want to read up on pointers first, though.

Resources