Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I have the following code:
int ver(unsigned char** v) {
unsigned char str1[] = "1.0.2";
strcpy(v, str1);
return 0;
}
int main(int argc, char* argv[]) {
unsigned char s[10];
ver(s);
printf("version = %s", s);
return 0;
}
I get the following warning that the pointer differs in signedness. Can you please advise.
Change the declaration of ver to:
int ver(unsigned char * v)
I.e. get rid of one of the * characters.
Warning about signedness is because strcpy and printf("%s") expect a char*, but you are passing unsigned char*
Also int ver(unsigned char ** v) should be int ver(char* v)
Actually the problem is this :
int ver(unsigned char ** v)
You have to change the function to that :
int ver(unsigned char * v)
You need to do 2 fixes:
1) Follow what others told you about the method declaration, changing to "int ver(unsigned char* v) {" ;
2) Just include <string.h> at your code.
Example:
#include <stdio.h>
#include <string.h>
int ver(unsigned char* v) {
unsigned char str1[] = "1.0.2";
strcpy(v, str1);
return 0;
}
int main(int argc, char* argv[]) {
unsigned char s[10];
ver(s);
printf("version = %s", s);
return 0;
}
The cause of the warning:
strcpy() expects a signed char*, and is called with a unsigned char*.
But there should be other warnings as well:
Function ver() expects a pointer to a pointer:
int ver(unsigned char ** v){
ver(s) sends a single pointer to char:
unsigned char s[10];
ver(s);
To solve, you can change the code to:
int ver(char *v) {
char str1[] = "1.0.2";
strcpy(v, str1);
return 0;
}
int main(int argc, char *argv[]){
char s[10];
ver(s);
printf("version = %s", s);
return 0;
}
Related
This question already has answers here:
returning a local variable from function in C [duplicate]
(4 answers)
Changing address contained by pointer using function
(5 answers)
Closed 1 year ago.
I am trying to implement a function as stated in the title. I think I am very close to solution but a problem.
input: 51% are admitted.
output: x:51 (null)
but output should have been:
s:% are admitted.
My try is here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int str2int(int);
int isdigit(int);
long str2double(int);
int driver(char *, char *);
int main(){
char *s = "51% are admitted.";
char *sPtr;
int x = driver(s, sPtr);
printf("x:%d sPtr:%s", x, sPtr);
return 0;
}
int isdigit(int ch){
return (ch>=48 && ch<=57)?1:0;
}
int str2int(int ch){
return ch-48;
}
int driver(char *s, char *sPtr){
int i=0, number=0;
while(s[i]!='\0' && isdigit(s[i])){
number = number*10 + str2int(s[i]);
i++;
}
sPtr=s+i;
printf("%s\n", sPtr);
return number;
}
The problem is, in main, sPtr seems as null but in driver function, sPtr is % is admitted which is what it should be. How can I fix the problem so that I can print the solution correctly without using a printf statement in driver function?
EDIT:
The problem is as #Johnny Mopp said, I was trying to pass a copy of that variable. Therefore, I need to pass the address of variable of *sPtr which appears char **sPtr in prototype. And the code should be:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int str2int(int);
int isdigit(int);
long str2double(int);
int driver(char *, char **);
int main(){
char *s = "51% are admitted.";
char **sPtr;
int x = driver(s, &sPtr);
printf("x:%d sPtr:%s", x, sPtr);
return 0;
}
int isdigit(int ch){
return (ch>=48 && ch<=57)?1:0;
}
int str2int(int ch){
return ch-48;
}
int driver(char *s, char **sPtr){
int i=0, number=0;
while(s[i]!='\0' && isdigit(s[i])){
number = number*10 + str2int(s[i]);
i++;
}
*sPtr=s+i;
return number;
}
Thanks for contributes of #Johnny Mopp and #paulsm4
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 3 years ago.
Improve this question
When I try and use atoi with an int and malloc I get a bunch of errors and key is given the wrong value, what am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct arguments {
int key;
};
void argument_handler(int argc, char **argv, struct arguments *settings);
int main(int argc, char **argv) {
argv[1] = 101; //makes testing faster
struct arguments *settings = (struct arguments*)malloc(sizeof(struct arguments));
argument_handler(argc, argv, settings);
free(settings);
return 0;
}
void argument_handler(int argc, char **argv, struct arguments *settings) {
int *key = malloc(sizeof(argv[1]));
*key = argv[1];
settings->key = atoi(key);
printf("%d\n", settings->key);
free(key);
}
You probably want this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct arguments {
int key;
};
void argument_handler(int argc, char** argv, struct arguments* settings);
int main(int argc, char** argv) {
argv[1] = "101"; // 101 is a string, therefore you need ""
struct arguments* settings = (struct arguments*)malloc(sizeof(struct arguments));
argument_handler(argc, argv, settings);
free(settings);
return 0;
}
void argument_handler(int argc, char** argv, struct arguments* settings) {
char* key = malloc(strlen(argv[1]) + 1); // you want the length of the string here,
// and you want char* here, not int*
strcpy(key, argv[1]); // string needs to be copied
settings->key = atoi(key);
printf("%d\n", settings->key);
free(key);
}
But this is very awkward, actually the argument_handler can be rewritten like this:
void argument_handler(int argc, char** argv, struct arguments* settings) {
settings->key = atoi(argv[1]);
printf("%d\n", settings->key);
}
Disclaimer: I only corrected what was obviously wrong, there are still checks that need to be done, e.g. if argc is smaller than 2 etc.
I'm new to C and I'm having a hard time understanding how to call methods with pointers. Currently this code should reverse a null-terminated string, but I get the errors
main.c:8:12: error: use of undeclared identifier 'sas'
char* N = sas;
^ main.c:10:10: warning: incompatible integer to pointer conversion passing 'char' to parameter of type
'char *'; remove * [-Wint-conversion]
reverse(*N);
^~ ./header.h:3:27: note: passing argument to parameter 'N' here EXTERN void reverse(char *N);
My actual code is this:
Main:
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(int argc, char *argv[])
{
char* N = sas;
reverse(*N);
}
reverse:
#include <stdio.h>
#include "header.h
#include <stdlib.h>
void reverse(char *str)
{
char* end = str;
char temp;
printf("this is *str: %c\n", *str);
if (str)
{
while (*end)
{
++end:
}
end--;
while (str < end)
{
temp = *str
*str++ = *end;
*end-- = temp;
}
}
}
header.h:
#define EXTERN extern
EXTERN void reverse(char *N)
thanks for the help and time!
int main(int argc, char *argv[])
{
char* N = "sas";
reverse(*N);
}
First you make N point to a string constant. Then you try to reverse what N points to. But since N points to a string constant, you're trying to reverse a string constant. By definition, constants cannot have their values changed.
First of all, there're many syntax errors within that piece of code:
'sas' what is that? your compiler thinks it's a variable but can't find any with that name. if you wanted to put a "sas" string, then:
char* N = "sas";
inconsistant brackets. More closing brackets than opening ones, and no opening bracket after declaring your function.
As I understand it, you are trying to reverse a string. This is just a slight modification of your code.
Full Source:
#include <stdio.h>
#include <stdlib.h>
void reverse(char *N);
int main(int argc, char *argv[])
{
char strSas[] = "sas";
reverse(strSas);
}
void reverse(char *str)
{
char* end = str;
char temp;
if(str) {
printf("this is *str: %c\n", *str);
while(*end) {
++end;
}
end--;
while(str < end) {
temp = *str;
*str = *end;
*end = temp;
++str;
--end;
}
}
}
So my question is that if I have the following code
main(){
char arr[1][3];
foo(arr);
}
void foo(char arr1[1][3]){
arr1[0] = "AB\0";
}
Does this mean that the value in arr from main would also be modified into "AB\0"?
Try checking this stack overflow question and answer, it is an age-old question
Passing an array by reference in C?
Why not test it out yourself?
#include <stdio.h>
void foo(char arr[]);
int main(int argc, char **argv)
{
char arr[5] = "test";
printf("%s\n", arr);
foo(arr);
printf("%s\n", arr);
return 0;
}
void foo(char arr[])
{
arr[0] = 'p';
}
This question already has answers here:
Assignment of function parameter has no effect outside the function
(2 answers)
Closed 9 years ago.
I am trying to understand why this statement doesn't work.
char resp[] = "123456789";
void getValue(char *im)
{
im = resp;
printf("\n%s\n",im);
}
int main(int argc, char *argv[])
{
char imei[11] = {0};
getValue(imei);
printf("\nIMEI: %s\n",imei);
return 0;
}
Output:
123456789
IMEI:
You can not assign with =, use strcpy instead:
#include <stdio.h>
#include <string.h>
char resp[] = "123456789";
void getValue(char *im)
{
im = strcpy(im, resp);
printf("\n%s\n",im);
}
int main(int argc, char *argv[])
{
char imei[11] = {0};
getValue(imei);
printf("\nIMEI: %s\n",imei);
return 0;
}
That's because imei is an array[11] (not just a pointer to), if you want to assign via = you can:
#include <stdio.h>
char resp[] = "123456789";
void getValue(char **im)
{
*im = resp;
printf("\n%s\n",*im);
}
int main(int argc, char *argv[])
{
char *imei; /* Not an array but a pointer */
getValue(&imei);
printf("\nIMEI: %s\n",imei);
return 0;
}
C passes parameters by value. Whatever change you ale to the im will be lost when the function exits. If you want to preserve the change. Pass the address of the pointer. Then you can change the pointer at the address you pass.
Try this:
char resp[] = "123456789";
void getValue(char **im)
{
*im = resp;
printf("\n%s\n",*im);
}
You need to pass a pointer to a pointer as your program argument.