warning: assignment makes integer from pointer without a cast
Asked Answered
C

5

41

When I declare a char * to a fixed string and reuse the pointer to point to another string

/* initial declaration */
char *src = "abcdefghijklmnop";
.....

/* I get the   "warning: assignment makes integer from pointer without a cast" */
*src ="anotherstring";

I tried to recast the pointer but no success.

Cauterant answered 25/2, 2011 at 20:25 Comment(1)
It is deprecated in C to use a non-const char* to point to string constants -- you should change src to be of type const char*.Fragmentary
P
36

The expression *src refers to the first character in the string, not the whole string. To reassign src to point to a different string tgt, use src = tgt;.

Predictory answered 25/2, 2011 at 20:27 Comment(0)
P
36

When you write the statement

*src = "anotherstring";

the compiler sees the constant string "abcdefghijklmnop" like an array. Imagine you had written the following code instead:

char otherstring[14] = "anotherstring";
...
*src = otherstring;

Now, it's a bit clearer what is going on. The left-hand side, *src, refers to a char (since src is of type pointer-to-char) whereas the right-hand side, otherstring, refers to a pointer.

This isn't strictly forbidden because you may want to store the address that a pointer points to. However, an explicit cast is normally used in that case (which isn't too common of a case). The compiler is throwing up a red flag because your code is likely not doing what you think it is.

It appears to me that you are trying to assign a string. Strings in C aren't data types like they are in C++ and are instead implemented with char arrays. You can't directly assign values to a string like you are trying to do. Instead, you need to use functions like strncpy and friends from <string.h> and use char arrays instead of char pointers. If you merely want the pointer to point to a different static string, then drop the *.

Portfire answered 25/2, 2011 at 21:10 Comment(0)
B
9

The warning comes from the fact that you're dereferencing src in the assignment. The expression *src has type char, which is an integral type. The expression "anotherstring" has type char [14], which in this particular context is implicitly converted to type char *, and its value is the address of the first character in the array. So, you wind up trying to assign a pointer value to an integral type, hence the warning. Drop the * from *src, and it should work as expected:

src = "anotherstring";

since the type of src is char *.

Bohaty answered 25/2, 2011 at 21:42 Comment(0)
E
0

What Jeremiah said, plus the compiler issues the warning because the production:

*src ="anotherstring";

says: take the address of "anotherstring" -- "anotherstring" IS a char pointer -- and store that pointer indirect through src (*src = ... ) into the first char of the string "abcdef..." The warning might be baffling because there is nowhere in your code any mention of any integer: the warning seems nonsensical. But, out of sight behind the curtain, is the rule that "int" and "char" are synonymous in terms of storage: both occupy the same number of bits. The compiler doesn't differentiate when it issues the warning that you are storing into an integer. Which, BTW, is perfectly OK and legal but probably not exactly what you want in this code.

-- pete

Excellency answered 25/2, 2011 at 20:52 Comment(1)
int and char typically don't have the same number of bits. char has usually been 8 bits throughout the history of C, and int is guaranteed to be at least 16 and is more likely to be 32 or more. They are both integral types, and therefore "integer" could mean either of them. This is a little surprising when the "integer" being referred to is a char, though.Bolten
S
0

structure of node:-

struct node {
  char key[10];
  struct node *left, *right;
};

Also it may arrive when you are doing something like this

struct node *newNode(char item[10]) {
  struct node *temp = (struct node *)malloc(sizeof(struct node));
  temp->key[10] = item;
  temp->left = temp->right = NULL;
  return temp;
}

It will show warning in line 3 and all of your code will be messed up. Good news is:-

you have to prove that it is an array and it can be simply done by

temp->key[10] = item[10];

As simple as that...

Stettin answered 9/3, 2022 at 2:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.