I have query on how to assign a pointer to a character
Sample program:
#include <stdio.h>
void main()
{
const char str1[]="MANCHESTER";
char *q;
q=str1;
*q='A';
printf("%s\n",q);
}
In the the above program, I am assigning a character pointer to string "str1",
When I compile the program with gcc, I get the below warning:
ptr3.c: In function ‘main’:
ptr3.c:7:3: warning: assignment discards qualifiers from pointer target type
I am not able to understand what the warning means.
warnings i get:
2: warning: return type of 'main' is not 'int'
: In function 'main':
7: warning: implicit declaration of function 'printf'
7: warning: incompatible implicit declaration of built-in function 'printf'
adding include stdio.h and return type int, fixes those.
#include <stdio.h>
int main()
{
char str1[]="MANCHESTER";
char *q;
q=str1;
*q='A';
printf("%s\n",q);
return 0;
}
edit:
i guess you should not declare the char array as const, if you plan on modifying it, directly or indirectly through another pointer
Related
#include <stdio.h>
int main(void) {
int arr[10];
arr = "Hello";
printf("%s",arr);
return 0;
}
The above code shows compiler error:
t.c: In function ‘main’:
t.c:5:9: error: assignment to expression with array type
arr = "Hello";
^
t.c:6:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat=]
printf("%s",arr);
^
Whereas the below code works fine.
#include <stdio.h>
int main(void) {
char arr[10] = "Hello";
printf("%s",arr);
return 0;
}
Both look identical to me. What am I missing here?
They are not identical.
First of all, it makes zero sense to initialize an int array with a string literal, and in worst case, it may invoke undefined behavior, as pointer to integer conversion and the validity of the converted result thereafter is highly platform-specific behaviour. In this regard, both the snippets are invalid.
Then, correcting the data type, considering the char array is used,
In the first case,
arr = "Hello";
is an assignment, which is not allowed with an array type as LHS of assignment.
OTOH,
char arr[10] = "Hello";
is an initialization statement, which is perfectly valid statement.
Don't know how your second code is working (its not working in my case PLEASE TELL ME WHAT CAN BE THE REASON) it is saying: array of inappropriate type (int) initialized with string constant
Since you can't just assign a whole string to a integer variable.
but you can assign a single character to a int variable like:
int a[5]={'a','b','c','d','d'}
This is my code
#include<stdio.h>
#include<stdlib.h>
void main() {
FILE *fp;
char * word;
char line[255];
fp=fopen("input.txt","r");
while(fgets(line,255,fp)){
word=strtok(line," ");
while(word){
printf("%s",word);
word=strtok(NULL," ");
}
}
}
This the warning I get.
token.c:10:7: warning: assignment makes pointer from integer without a cast [enabled by default]
word=strtok(line," ");
^
token.c:13:8: warning: assignment makes pointer from integer without a cast [enabled by default]
word=strtok(NULL," ");
^
The word is declared as char*. Then why this warning arises?
Include #include <string.h> to get the prototype for strtok().
Your compiler (like in pre-C99 C) assumed strtok() returns an int because of that. But not providing function declaration/prototype is not valid in modern C (since C99).
There used an old rule in C which allowed implicit function declarations. But implicit int rule has been removed from C language since C99.
See: C function calls: Understanding the "implicit int" rule
strtok() is prototyped in <string.h>, you need to include it.
Otherwise, with the lack of forward declaration, your compiler assumes that any function used for which the signature is not known to it, returns an int and accepts any number of parameters. This was called implicit declarration .
FWIW, implicit function declaration is now invalid as per the latest standard. Quoting C11, Foreward, "Major changes in the second edition included:"
remove implicit function declaration
You need to include string.h
#include <string.h>
Why does the below code cause a run-time crash?
The code itself is not very useful, but , by creating a pointer to a char pointer and pointing to string literals in main, passing this pointer to my function and trying to read the strings causes problems. Why is that exactly?
By creating an array of strings instead in main however (commented out) , there are no problems in passing and reading the strings. Thanks in advance for your knowledge.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* GetString(char** strs, int strsSize);
char* GetString(char** strs, int strsSize)
{
return *strs;
}
int main()
{
char** stringArr = {"ab", "abc", "abcd"};
//char* stringArr [] = {"ab", "abc", "abcd"};
char* resultStr;
resultStr = GetString(stringArr, 3);
printf("%s\n", resultStr);
return 0;
}
The initializer for stringArr is not valid. You have a pointer to a pointer, not an array. A pointer cannot be initialized with the {} syntax.
When compiling with -Wall -Wextra, the following warnings are produced:
/tmp/x1.c: In function ‘main’:
/tmp/x1.c:15: warning: initialization from incompatible pointer type
/tmp/x1.c:15: warning: excess elements in scalar initializer
/tmp/x1.c:15: warning: (near initialization for ‘stringArr’)
/tmp/x1.c:15: warning: excess elements in scalar initializer
/tmp/x1.c:15: warning: (near initialization for ‘stringArr’)
The commented out declaration you have is the correct one.
Both declarations are valid to be passed to GetString because an array decays into a pointer to the first element when passed to a function. However, a pointer and an array are not the same thing.
I'm trying to return a pointer to a character array. The function is located in a different file. I compile the files together and it prints out "hello" just fine, but still produces a warning
warning: assignment makes pointer from integer without a cast [enabled by default]
I tried casting the returned value of function() to (char *), but ended up with a different warning:
warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]" instead.
What am I doing wrong?
File 1
#include <stdio.h>
int main()
{
printf("%s\n", function());
return 0;
}
File 2
#include <stdio.h>
char *message = "hello";
char *function()
{
return(message);
}
The problem here is a missing prototype. For historical reasons C lets you use functions without declaring them. However, all such functions are considered returning int, and all their parameters are considered int as well.
Since your function returns char*, you need to add a prototype for it (it's a good idea to have prototypes for all your functions, including ones that indeed return an int):
#include <stdio.h>
char *function(); // <<== Add this line
int main()
{
printf("%s\n", function());
return 0;
}
I want to copy arr2 to arr and pass arr as a function paramater
void func(char * array)
{}
int main(void)
{
int a;
char arr[6][50];
char arr2[][50]={"qweeeaa","bbbb","ffaa","eeaa","aaaa","ffaa"};
for(a=0; a<6;a++)
{
strcpy(arr[a], arr2[a]);
}
func(arr);
return 0;
}
But I can not pass arr as a function parameter. I get
[Warning] passing argument 1 of 'func' from incompatible pointer type [enabled by default]
[Note] expected 'char *' but argument is of type 'char (*)[50]'
I am using MinGW GCC 4.8.1
The type you pass and the type the function expects do not match. Change the function to receive a pointer to an array:
void func(char (*array)[50])
{
}
Other problems:
1) You haven't declared a prototype for func() either. In pre-C99 mode, compiler would assume the function returns an int and this would cause problem.
In C99 and C11, missing prototype makes your code invalid. So either declare the prototype at the top of the source file or move the function above main().
2) Include appropriate headers (<stdio.h> for printf etc).