• Stack Overflow Public questions & answers
  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Talent Build your employer brand
  • Advertising Reach developers & technologists worldwide
  • About the company

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

C pointers and arrays: [Warning] assignment makes pointer from integer without a cast [closed]

I'm having some trouble with pointers and arrays in C. Here's the code:

When I compile and run the code I get this warning:

[Warning] assignment makes pointer from integer without a cast [enabled by default]

For line number 9 (ap = a[4];) and the terminal crashes. If I change line 9 to not include a position (ap = a;) I don't get any warnings and it works. Why is this happening? I feel like the answer is obvious but I just can't see it.

user2274889's user avatar

3 Answers 3

In this case a[4] is the 5th integer in the array a , ap is a pointer to integer, so you are assigning an integer to a pointer and that's the warning. So ap now holds 45 and when you try to de-reference it (by doing *ap ) you are trying to access a memory at address 45, which is an invalid address, so your program crashes.

You should do ap = &(a[4]); or ap = a + 4;

In c array names decays to pointer, so a points to the 1st element of the array. In this way, a is equivalent to &(a[0]) .

Dipto's user avatar

What are you doing: (I am using bytes instead of in for better reading)

You start with int *ap and so on, so your (your computers) memory looks like this:

lets take a look waht happens when (print short cut for ...print("$d", ...)

and so on, so a[0] is the same as *a, a[1] = *(a+1), ....

a[n] just reads easier.

now, what happens at line 9?

So the "warning" is not just a warning it's a severe error.

halfbit's user avatar

int[] and int* are represented the same way, except int[] allocates (IIRC).

ap is a pointer, therefore giving it the value of an integer is dangerous, as you have no idea what's at address 45.

when you try to access it ( x = *ap ), you try to access address 45, which causes the crash, as it probably is not a part of the memory you can access.

njzk2's user avatar

Not the answer you're looking for? Browse other questions tagged c arrays pointers warnings or ask your own question .

Hot Network Questions

assignment makes pointer from integer without a cast c programming

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

C Board

Home

Assignment makes pointer from integer without a cast

Thread: Assignment makes pointer from integer without a cast

Thread tools.

Search Thread

deciel is offline

I keep getting this error message: "warning: assignment makes pointer from integer without a cast" for line 17 (which I've made red). I've gotten this message a few times before, but I can't remember how I fixed it, and I can't figure out how to fix this one. Any suggestions? Also, it's a code that will take a password entered by the user and then run several for loops until it matches the password. It prints what it's figured out each time it guesses a new letter. Code: #include <stdio.h> #include <string.h> int main(void) { int i, j; char password[25]; char cracked[25]; char *p; char guess = '!'; printf("Enter a password of 25 characters or less: \n"); scanf("%s", password); printf("Password is being cracked..."); for (i = 0, p = password[i]; i < 25; i++, p++) { for(j = 0; j < 90; j++) { if (*p == guess) { strcpy(p, cracked); printf("\t %s \n"); break; } guess++; } //end <search> for loop } //end original for loop return 0; }
Last edited by deciel; 12-13-2011 at 01:57 AM .

JohnGraham is offline

Code: for (i = 0, p = password[i]; i < 25; i++, p++) password[i] is the value at index i of password . You want the address of said value, so you want p = &password[i] (or, equivalently, p = password + i ).
Oh! Thank you, it worked!

sparkomemphis is offline

Originally Posted by deciel I keep getting this error message: "warning: for (i = 0, p = password[i]; i < 25; i++, p++) [/CODE] Note: password[i] == password[0] == *password since this is the assignment portion of for loop and i is set to zero (0).

Tclausex is offline

If you want to set a pointer to the beginning of an array, just use Code: p = password An array name is essentially a pointer to the start of the array memory. Note, for a null terminated string, you could just test for Code: *p //or more explicitly *p == '\0' Also, a 25-element char array doesn't have room for a 25 character string AND a null terminator. And, ask yourself, what's going on when I enter, say a 10 character password, and i > 10.

subscribe to a feed

Similar Threads

Assignment makes pointer from integer without a cast, warning: assignment makes integer from pointer without a cast, warning: assignment makes integer from pointer without a cast, ' assignment makes pointer from integer without a cast ".

C: warning assignment makes integer from pointer without a cast

Answers and Replies

"RED" is not a character variable - it is a string. The pointer conversion is from the string pointer.  

jedishrfu said: choice should be defined as: char *choice;
D H said: That should be const char* choice , not just char* choice . Assigning to a pointer to a string such as "RED" is illegal (undefined behavior), so it's best to make the pointer a type that does not accept assignments.
jedishrfu said: Doesn't this depend on what he's trying to do? Suppose this choice is in some sort of input loop where first its RED and then its BLUE ...
D H said: Assigning to a pointer to a string such as "RED" is illegal (undefined behavior), so it's best to make the pointer a type that does not accept assignments.
D H said: There's nothing wrong with that. const char * choice (or char const * choice , same thing) means that choice can be used on the left hand side of an assignment statement but that choice[1] cannot. You are apparently thinking of char * const choice = "RED"; , but that's a completely different data type. With this declaration, choice can only be assigned a value in the declaration statement. However, choice[1]='D'; is perfectly legal with this declaration. You can combine the two restrictions with the declaration const char * const choice = "RED";
rcgldr said: It's my understanding that literal strings are like statics, and exist from start to termination of a program, so why should assigning a pointer to a literal string be undefined? Trying to change a value in the literal string via the pointer would be illegal / undefined behavior, but the pointer assignment shouldn't be an issue.
nsaspook said: It depends on the computer architecture and how strict the compiler is.
rcgldr said: The point I was making is how string literals are defined in the C89 and later standards. From the C89 standard, section 3.1.4: ... So it would seem that only an attempt to modify a string is undefined, not the usage of a pointer to access a literal string. The type "array of char" or "array of wchar_t" would be the same regardless of where the string literal was stored (ROM, RAM, code section of a program, ... ).
D H said: I wasn't clear with my previous post. There's nothing wrong per se with char* ptr; ...; ptr="RED"; ...; ptr="BLUE"; What's wrong is assigning into that pointer: *ptr = 'A'; . What's worse is that most compilers won't report this as an error. You don't find out until runtime. Declaring the variable as a const char* pointer (or char const* , same thing) and assignments such as ptr="RED"; are still legal, but now *ptr = 'A'; becomes a compile-time error.

Suggested for: C: warning assignment makes integer from pointer without a cast

Hot Threads

Recent Insights

IMAGES

  1. Assignment makes integer from pointer without a cast by Johnson Jessica

    assignment makes pointer from integer without a cast c programming

  2. Assignment makes pointer from integer without a cast by Contreras Celeste

    assignment makes pointer from integer without a cast c programming

  3. shmoop argumentative essay

    assignment makes pointer from integer without a cast c programming

  4. How to display the value of an integer variable through pointer ?

    assignment makes pointer from integer without a cast c programming

  5. [Solved]-'printf' makes pointer from integer without a cast-C

    assignment makes pointer from integer without a cast c programming

  6. Assignment makes integer from pointer without a cast victorious

    assignment makes pointer from integer without a cast c programming

VIDEO

  1. C program to find a number is odd or even. || Basic C language

  2. 38 Function pointers in structs OOP like code

  3. Addition of two numbers using pointer in c++ || study with me

  4. Pointers // C programming example 23 #shorts #programming #coding

  5. MEG 10

  6. write a program to find substraction by #pointer concept in #c programming language #coder #shorts

COMMENTS

  1. c

    A char is a form of integer in C. You are assigning it into a char [] which is a pointer. Hence "converting integer to pointer". Your strToLower makes all its changes in place, there is no reason for it to return anything, especially not a char. You should "return" void, or a char*.

  2. C pointers and arrays: [Warning] assignment makes pointer

    I'm having some trouble with pointers and arrays in C. Here's the code: #include<stdio.h> int *ap; int a [5]= {41,42,43,44,45}; int x; int main () { ap = a [4]; x = *ap; printf ("%d",x); return 0; } When I compile and run the code I get this warning: [Warning] assignment makes pointer from integer without a cast [enabled by default]

  3. Assignment makes pointer from integer without a cast

    If you want to set a pointer to the beginning of an array, just use Code: ? 1 p = password An array name is essentially a pointer to the start of the array memory. Note, for a null terminated string, you could just test for Code: ? 1 2 3 *p //or more explicitly *p == '\0'

  4. What does 'warning: assignment makes integer from pointer

    What does "warning: assignment makes integer from pointer without a cast [Wint conversion]" mean in C (arrays, pointers, struct, warnings)? Ad by JetBrains Enjoy productive Java with IntelliJ IDEA. Discover instant and clever code completion, on-the-fly code analysis, and reliable refactoring tools. Download 6 Answers Best Maurice Bourdin

  5. C: warning assignment makes integer from pointer without a cast

    Assigning to a pointer to a string such as "RED" is illegal (undefined behavior), so it's best to make the pointer a type that does not accept assignments. Doesn't this depend on what he's trying to do? Suppose this choice is in some sort of input loop where first its RED and then its BLUE ... Sep 13, 2013 #6 nsaspook Science Advisor 1,207 2,278