Ubuntu Forums

  • Unanswered Posts
  • View Forum Leaders
  • Contact an Admin
  • Forum Council
  • Forum Governance
  • Forum Staff

Ubuntu Forums Code of Conduct

  • Forum IRC Channel
  • Get Kubuntu
  • Get Xubuntu
  • Get Lubuntu
  • Get Ubuntu Studio
  • Get Ubuntu Cinnamon
  • Get Edubuntu
  • Get Ubuntu Unity
  • Get Ubuntu Kylin
  • Get Ubuntu Budgie
  • Get Ubuntu Mate
  • Ubuntu Code of Conduct
  • Ubuntu Wiki
  • Community Wiki
  • Launchpad Answers
  • Ubuntu IRC Support
  • Official Documentation
  • User Documentation
  • Distrowatch
  • Bugs: Ubuntu
  • PPAs: Ubuntu
  • Web Upd8: Ubuntu
  • OMG! Ubuntu
  • Ubuntu Insights
  • Planet Ubuntu
  • Full Circle Magazine
  • Activity Page
  • Please read before SSO login
  • Advanced Search

Home

  • The Ubuntu Forum Community
  • Ubuntu Specialised Support
  • Development & Programming
  • Programming Talk

[SOLVED] C - assigment makes integer from pointer without a cast warning

Thread: [solved] c - assigment makes integer from pointer without a cast warning, thread tools.

  • Show Printable Version
  • Subscribe to this Thread…
  • View Profile
  • View Forum Posts
  • Private Message

usernamer is offline

I know it's a common error, and I've tried googling it, looked at a bunch of answers, but I still don't really get what to do in this situation.... Here's the relevant code: Code: #include <stdio.h> #include <stdlib.h> #include <string.h> int main( int argc, char *argv[] ) { char path[51]; const char* home = getenv( "HOME" ); strcpy( path, argv[1] ); path[1] = home; return 0; } -- there is more code in the blank lines, but the issue's not there (I'm fairly sure), so didn't see the point in writing out 100 odd lines of code. I've tried some stuff like trying to make a pointer to path[1], and make that = home, but haven't managed to make that work (although maybe that's just me doing it wrong as opposed to wrong idea?) Thanks in advance for any help

r-senior is offline

Re: C - assigment makes integer from pointer without a cast warning

path[1] is the second element of a char array, so it's a char. home is a char *, i.e. a pointer to a char. You get the warning because you try to assign a char* to a char. Note also the potential to overflow your buffer if the content of the argv[1] argument is very long. It's usually better to use strncpy.
Last edited by r-senior; March 10th, 2013 at 03:03 PM . Reason: argv[1] would overflow, not HOME. Corrected to avoid confusion.
Please create new threads for new questions. Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].
  • Visit Homepage

Christmas is offline

You can try something like this: Code: #include <stdio.h> #include <stdlib.h> #include <string.h> int main( int argc, char *argv[] ) { char *path; const char *home = getenv("HOME"); path = malloc(strlen(home) + 1); if (!path) { printf("Error\n"); return 0; } strcpy(path, home); printf("path = %s\n", path); // if you want to have argv[1] concatenated with path if (argc >= 2) { path = malloc(strlen(home) + strlen(argv[1]) + 1); strcpy(path, argv[1]); strcat(path, home); printf("%s\n", path); } // if you want an array of strings, each containing path, argv[1]... char **array; int i; array = malloc(argc * sizeof(char*)); array[0] = malloc(strlen(home) + 1); strcpy(array[0], home); printf("array[0] = %s\n", array[0]); for (i = 1; i < argc; i++) { array[i] = malloc(strlen(argv[i]) + 1); strcpy(array[i], argv[i]); printf("array[%d] = %s\n", i, array[i]); } // now array[i] will hold path and all the argv strings return 0; } Just as above, your path[51] is a string while path[1] is only a character, so you can't use strcpy for that.
Last edited by Christmas; March 10th, 2013 at 09:51 PM .
TuxArena - Ubuntu/Debian/Mint Tutorials | Linux Stuff Intro Tutorials | UbuTricks I play Wesnoth sometimes. And AssaultCube .
Originally Posted by Christmas You can try something like this: Code: #include <stdio.h> #include <stdlib.h> #include <string.h> int main( int argc, char *argv[] ) { char *path; const char *home = getenv("HOME"); path = malloc(strlen(home) + 1); if (!path) { printf("Error\n"); return 0; } strcpy(path, home); printf("path = %s\n", path); // if you want to have argv[1] concatenated with path if (argc >= 2) { path = malloc(strlen(home) + strlen(argv[1]) + 1); strcpy(path, argv[1]); strcat(path, home); printf("%s\n", path); } // if you want an array of strings, each containing path, argv[1]... char **array; int i; array = malloc(argc * sizeof(char*)); array[0] = malloc(strlen(home) + 1); strcpy(array[0], home); printf("array[0] = %s\n", array[0]); for (i = 1; i < argc; i++) { array[i] = malloc(strlen(argv[i]) + 1); strcpy(array[i], argv[i]); printf("array[%d] = %s\n", i, array[i]); } // now array[i] will hold path and all the argv strings return 0; } Just as above, your path[51] is a string while path[1] is only a character, so you can't use strcpy for that. Excellent point. I've basically fixed my problem by reading up on pointers again (haven't done any C for a little while, so forgot some stuff), and doing: Code: path[1] = *home; the code doesn't moan at me when I compile it, and it runs okay (for paths which aren't close to 51 at least), but after reading what you read, I just wrote a quick program and found out that getenv("HOME") is 10 characters long, not 1 like I seem to have assumed, so I'll modify my code to fix that.
Yes, getenv will return the path to your home dir, for example /home/user, but path[1] = *home will still assign the first character of home to path[1] (which would be '/').
  • Private Messages
  • Subscriptions
  • Who's Online
  • Search Forums
  • Forums Home
  • New to Ubuntu
  • General Help
  • Installation & Upgrades
  • Desktop Environments
  • Networking & Wireless
  • Multimedia Software
  • Ubuntu Development Version
  • Virtualisation
  • Server Platforms
  • Ubuntu Cloud and Juju
  • Packaging and Compiling Programs
  • Development CD/DVD Image Testing
  • Ubuntu Application Development
  • Ubuntu Dev Link Forum
  • Bug Reports / Support
  • System76 Support
  • Apple Hardware Users
  • Recurring Discussions
  • Mobile Technology Discussions (CLOSED)
  • Announcements & News
  • Weekly Newsletter
  • Membership Applications
  • The Fridge Discussions
  • Forum Council Agenda
  • Request a LoCo forum
  • Resolution Centre
  • Ubuntu/Debian BASED
  • Arch and derivatives
  • Fedora/RedHat and derivatives
  • Mandriva/Mageia
  • Slackware and derivatives
  • openSUSE and SUSE Linux Enterprise
  • Gentoo and derivatives
  • Any Other OS
  • Assistive Technology & Accessibility
  • Art & Design
  • Education & Science
  • Documentation and Community Wiki Discussions
  • Outdated Tutorials & Tips
  • Ubuntu Women
  • Arizona Team - US
  • Arkansas Team - US
  • Brazil Team
  • California Team - US
  • Canada Team
  • Centroamerica Team
  • Instalación y Actualización
  • Colombia Team - Colombia
  • Georgia Team - US
  • Illinois Team
  • Indiana - US
  • Kentucky Team - US
  • Maine Team - US
  • Minnesota Team - US
  • Mississippi Team - US
  • Nebraska Team - US
  • New Mexico Team - US
  • New York - US
  • North Carolina Team - US
  • Ohio Team - US
  • Oklahoma Team - US
  • Oregon Team - US
  • Pennsylvania Team - US
  • Texas Team - US
  • Uruguay Team
  • Utah Team - US
  • Virginia Team - US
  • West Virginia Team - US
  • Australia Team
  • Bangladesh Team
  • Hong Kong Team
  • Myanmar Team
  • Philippine Team
  • Singapore Team
  • Albania Team
  • Catalan Team
  • Portugal Team
  • Georgia Team
  • Ireland Team - Ireland
  • Kenyan Team - Kenya
  • Kurdish Team - Kurdistan
  • Lebanon Team
  • Morocco Team
  • Saudi Arabia Team
  • Tunisia Team
  • Other Forums & Teams
  • Afghanistan Team
  • Alabama Team - US
  • Alaska Team - US
  • Algerian Team
  • Andhra Pradesh Team - India
  • Austria Team
  • Bangalore Team
  • Bolivia Team
  • Cameroon Team
  • Colorado Team - US
  • Connecticut Team
  • Costa Rica Team
  • Ecuador Team
  • El Salvador Team
  • Florida Team - US
  • Galician LoCo Team
  • Hawaii Team - US
  • Honduras Team
  • Idaho Team - US
  • Iowa Team - US
  • Jordan Team
  • Kansas Team - US
  • Louisiana Team - US
  • Maryland Team - US
  • Massachusetts Team
  • Michigan Team - US
  • Missouri Team - US
  • Montana Team - US
  • Namibia Team
  • Nevada Team - US
  • New Hampshire Team - US
  • New Jersey Team - US
  • Northeastern Team - US
  • Panama Team
  • Paraguay Team
  • Quebec Team
  • Rhode Island Team - US
  • Senegal Team
  • South Carolina Team - US
  • South Dakota Team - US
  • Switzerland Team
  • Tamil Team - India
  • Tennessee Team - US
  • Trinidad & Tobago Team
  • Uganda Team
  • United Kingdom Team
  • US LoCo Teams
  • Venezuela Team
  • Washington DC Team - US
  • Washington State Team - US
  • Wisconsin Team
  • Za Team - South Africa
  • Zimbabwe Team

Tags for this Thread

View Tag Cloud

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is Off
  • HTML code is Off
  • Ubuntu Forums

C语言assignment makes pointer from integer without a cast

assignment to char from void makes integer from pointer without a cast wint conversion

这个警告的意思是将一个int整数值直接赋值给了一个指针变量。( 重点是类型不一致 )

消除警告的方法就是明确类型转换是否是正确的,如果确实要把整数变量赋予指针变量,那么请使用强制类型转换。否则,请用相同的数据类型,这样编译器就不会显示警告。

比如: int *p = 10;   //这就会产生这个警告

                                //因为 p 是指针变量,存放的是地址。而10是一个整数常量

改成: int *p = (int *)10    //强制转换成同一类型就可以消除警告

                                        //强制类型转换,10强制转换成了一个地址

assignment to char from void makes integer from pointer without a cast wint conversion

“相关推荐”对你有帮助么?

assignment to char from void makes integer from pointer without a cast wint conversion

请填写红包祝福语或标题

assignment to char from void makes integer from pointer without a cast wint conversion

你的鼓励将是我创作的最大动力

assignment to char from void makes integer from pointer without a cast wint conversion

您的余额不足,请更换扫码支付或 充值

assignment to char from void makes integer from pointer without a cast wint conversion

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。 2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

assignment to char from void makes integer from pointer without a cast wint conversion

Position Is Everything

Makes Pointer From Integer Without a Cast: Fix It Now!

  • Recent Posts

Melvin Nolan

  • How to Add Bullet Points in PowerPoint: A Step-by-Step Guide - May 25, 2024
  • Where is Slide Master in PowerPoint: A Comprehensive Guide for Beginners - May 25, 2024
  • How to Use Copilot in PowerPoint: A Step-by-Step Guide - May 25, 2024

Makes Pointer From Integer Without a Cast

Our research team has ensured that this will be your best resource on these error messages, and you won’t have to read another article about it again. With that said, launch your code that’s showing the error messages, and let’s fix it for you.

JUMP TO TOPIC

– You Assigned an Integer to a Pointer

– you want to convert an integer to a pointer, – you passed a variable name to the “printf()” function, – you used “struct _io_file” in a wrong way, – you copied a string to an invalid location, – you’re setting a pointer to a different type, – use equal data types during assignment, – ensure the pointer and integer have the same sizes, – pass a “format string” to the “printf()” function, – use a function that returns a pointer to “struct _io_file”, – copy the string to character array or character pointer, – assign pointers of compatible types, why your code made a pointer from an integer without a cast.

Your code made a pointer from an integer without a cast because you assigned an integer to a pointer or you want to convert an integer to a pointer. Other causes include the passing of a variable name to the “printf()” function and using “struct _io_file in the wrong way.

Finally, the following are also possible causes:

  • You copied a string to an invalid location
  • You’re setting a pointer to a different type

If you assign an integer to a pointer, it will lead to the “ assignment makes pointer from integer without a cast c programming ” error. For example, in the following, the “num_array” variable is an unsigned integer type while “tmp_array” is a pointer.

Later, an error will occur when the “for” loop tries to copy the values of the “num_array” to the “tmp_array” pointer using a variable assignment.

Makes Pointer From Integer Without a Cast Causes

For example, in the following, the “theta” variable is an integer while “ptr_theta” is a pointer. Both have different sizes, and you can’t convert the integer to a pointer.

If you pass a variable name to the “printf()” function, that’s when the compiler will throw the “ passing argument 1 of ‘printf’ makes pointer from integer without a cast ” error.

For example, in the following, “num_val” is an integer variable, and the code is trying to print it using the “printf()” function.

When you assign an integer to a pointer of “struct _io_file”, that’s when you’ll get the “ assignment to file aka struct _io_file from int makes pointer from integer without a cast ” error. For example, in the following code, “FILE” is a “typedef” for “struct _io_file”. This makes it a pointer to “struct _io_file”.

Later, the code assigned it to the integer “alpha,” and this will lead to an error stated below:

Makes Pointer From Integer Without a Cast Reasons

Now, in the following, the code is trying to copy a string (“source”) into an integer (“destination”), and this leads to an error because it’s not a valid operation:

When your code sets a pointer to a different type, your compiler will show the “ incompatible pointer type ” error. In the following code, “pacifier” is an integer pointer, while “qwerty” is a character pointer.

Both are incompatible, and your C compiler will not allow this or anything similar in your code.

How To Stop a Pointer Creation From an Integer Without a Cast

You can stop a pointer creation from an integer without a cast if you use equal data types during the assignment or ensure the integer and pointer have the same sizes. What’s more, you can pass a “format string” to “printf()” and use a function that returns a pointer to “struct _io_file”.

  • Copy the string to a character array or character pointer
  • Assign pointers of compatible types

During a variable assignment, use variables of equal data types. In our first example, we assigned a pointer (uint8_t *tmp_array) to an unsigned integer (uint8_t num_array) and it led to a compile-time error.

Now, the following is the revised code, and we’ve changed “tmp_array” to a real array. This will prevent the “ gcc warning: assignment makes pointer from integer without a cast ” error during compilation.

Makes Pointer From Integer Without a Cast Fixes

Now, the fix is to use “intptr_t” from the “stdint.h” header file because it guarantees that the pointer and integer will have the same sizes. We’ve used it in the following code, and you can compile it without an error.

To fix the “pointer to integer without a cast” in the “printf()” function, pass a “format string” as its first argument. How you write the “format string” depends on the variable that you’ll print. In the following updated code, “num_val” is an integer, so the “format string” is “%d”.

When you’re using “FILE” from the “stdlib.h” header file, you can prevent any “pointer from integer” error if you use a function that returns a pointer to “struct _io_file”.

An example of such a function is “fopen()” which allows you to open a file in C programming. Now, the following is a rewrite of the code that causes the pointer error in “struct _io_file”. This time, we use “fopen()” as a pointer to “FILE”.

Makes Pointer From Integer Without a Cast Solutions

Now, in the following code, we’ve changed “destination” from an integer to a “character array”. This means “strcpy()” can copy a string into this array without an error.

Meanwhile, the following is another version that turns the “destination” into a “character pointer”. With this, you’ll need to allocate memory using the “malloc()” function from the “stdlib.h” header file.

When you’re assigning pointers, ensure that they’re compatible by changing their data type . The following is the updated code for the “charlie” example , and “qwerty” is now an integer.

This article explained why your code made a pointer without a cast and how you can fix it. The following is a summary of what we talked about:

  • An attempt to convert an integer to a pointer will lead to the “makes pointer from integer without a cast wint conversion” error.
  • To prevent an “incompatible pointer type” error, don’t set a pointer to a different type.
  • If you’re using the “printf()” function, and you want to prevent any “pointer to integer” error, always pass a “format specifier”.

At this stage, you’ll be confident that you can work with integers and pointers in C programming without an error. Save our article, and share it with your developer communities to help them get rid of this error as well.

Related posts:

  • Input String Was Not in a Correct Format: Working Solutions
  • Best solutions to get rid of “operand should contain 1 column(s)” error
  • Another Git Process Seems To Be Running in This Repository: Solutions
  • Linker Command Failed With Exit Code 1: Causes and Fixes of This Error
  • Valueerror: No Json Object Could Be Decoded: Fix This Python Error
  • Conversion Failed When Converting Date And/or Time From Character String.
  • Importerror: Libcublas.So.9.0: Cannot Open Shared Object File: No Such File or Directory
  • Exception Code 0xe0434352: Read or Let It Disturb You
  • Noclassdeffounderror: An Article That Explains the Details
  • HTTP Error 500.19 – Internal Server Error: Fixing the Issue
  • Libressl SSL_connect: SSL_error_syscall in Connection to Github.com:443
  • Kudu Giving Forbidden Error 403: The Necessary Guide

Leave a Comment Cancel reply

Save my name, email, and website in this browser for the next time I comment.

  • 気になるQ&A

assignment to char from void makes integer from pointer without a cast wint conversion

  • アプリでもっと教えて!goo

dポイントプレゼントキャンペーン実施中!

  • 教えて!goo  >
  • コンピューター・テクノロジー  >
  • プログラミング・Web制作  >

入力中の回答があります。ページを離れますか?

※ページを離れると、回答が消えてしまいます

入力中のお礼があります。ページを離れますか?

※ページを離れると、お礼が消えてしまいます

assignment to char from void makes integer from pointer without a cast wint conversion

C言語のポインターに関する警告

  • 質問者: arm34fsa
  • 質問日時: 2008/12/11 12:08

line[100]で 「1」が格納されていたら「a」 「2」が格納されていたら「b」 「3」が格納されていたら「c」 とout[100]に代入する関数を作りたいのですが コンパイルすると関数の部分で warning: assignment makes integer from pointer without a cast という警告がでます。 ポインターは使っていないのですが、ポインターに関する警告が出ているようで困っています。 どこが悪いのかまったくわからなくて作業が完全に止まってしまいました。 解決法をおしえてください。お願いします。 /*宣言*/ int=i; /*main関数内のfor文で使用*/ char line[100], out[100]; void change(int); /*関数*/ void change(int i)   {    if(line[i]=='1'){     out[10]="a\0"    }if(line[i]=='2'){     out[10]="b\0";    }if(line[i]=='3'){     out[10]="c\0" } }

この質問への回答は締め切られました。

A 同じカテゴリの新着質問

A 回答   (2件)

assignment to char from void makes integer from pointer without a cast wint conversion

No.1 ベストアンサー

  • 回答者: chie65536
  • 回答日時: 2008/12/11 12:22

>    out[10]="a\0"

good

回答ありがとうございます。 "a\0"という書き方がまずかったのですか。 以後気をつけます。

assignment to char from void makes integer from pointer without a cast wint conversion

  • 回答者: buriburi3
  • 回答日時: 2008/12/11 12:27

char型の領域 out[10] に

まだまだ勉強中の身で「strcpy」というのを、今回はじめて知りました。 とても役に立つ知識をおしえていただきありがとうございます。

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

似たような質問が見つかりました

  • C言語・C++・C# c言語の問題です 課題1 (二分探索木とセット) 大きさ size の配列 array を考える。す 2 2023/01/10 21:08
  • C言語・C++・C# C言語のエラーについて 2 2022/07/11 13:56
  • C言語・C++・C# 10人分の生徒の英語の点数{32,34,41,38,40,26,14,46,42,50} と数学の点 2 2022/05/26 21:31
  • C言語・C++・C# c言語の問題です 3 2023/01/10 16:15
  • C言語・C++・C# C言語の課題が出たのですが自力でやっても分かりませんでした。 要素数がnであるint型の配列v2の並 3 2022/11/19 17:41
  • C言語・C++・C# プログラミング c言語 4 2023/03/07 01:05
  • C言語・C++・C# C# DatagridviewにExcelシートを反映するとエラーが出る 2 2023/05/06 17:12
  • C言語・C++・C# 宣言する関数の形が決まっている状態で、 str1とstr2の文字列をこの順に引っ付けてstrに保存し 2 2022/05/30 18:21
  • C言語・C++・C# c言語 プログラムのエラー 1 2023/02/11 20:31
  • C言語・C++・C# c言語でユーザ関数を利用して入力された文字列を反転させるプログラムを作りたいです。 3 2023/01/29 19:47

このQ&Aを見た人はこんなQ&Aも見ています

assignment to char from void makes integer from pointer without a cast wint conversion

風水の観点で選ぶ観葉植物とは?置き場所や上げたい運気ごとの注意点を紹介!

観葉植物で運気をアップするコツを、風水デザイン1級建築士の福島昌彦さんに伺った。

assignment to char from void makes integer from pointer without a cast wint conversion

C言語初心者の質問失礼します。

assignment to char from void makes integer from pointer without a cast wint conversion

ポインター引数の関数でコンパイルエラーが出る。

assignment to char from void makes integer from pointer without a cast wint conversion

main.c:7:43: warning: implicit declaration of func

assignment to char from void makes integer from pointer without a cast wint conversion

エラーの意味は? Lvalue required

assignment to char from void makes integer from pointer without a cast wint conversion

Enterキーを押されたら次の処理に移るという事をしたい。

gcc: incompatible pointer type

構造体のメンバをfor文で回したい

【gcc・cygwin】multiple definitionエラーの解決法

<unistd.h>をVisualStudioでつかえるようにする

c言語です コンパイルした時に出るNOTEとはなんですか??

C言語のポインターで詰まっている

C言語---ファイルに出力したデータをすべて消去する方法

文字列から空白を取り除きたいのですが

構造体の各データの表示について以下のようなプログラムを作成しました。

その他(プログラミング・Web制作)

 ポインタを使って関数の値のやり取り

C言語 exitの使い方

関連するカテゴリからQ&Aを探す

Visual basic(vba), microsoft asp.

ページトップ

  • ・ dポイントプレゼントキャンペーン実施中!
  • ・ 店舗&オフィスのプロのセキュリティ術
  • ・ プロがご自宅のセキュリティ指導
  • ・ 漫画をレンタルでお得に読める!

Q&Aの参照履歴

このQ&Aを見た人がよく見るQ&A

  • C言語 配列の長さの上限
  • C言語で構造体のメンバを簡単に...
  • 構造体の要素すべてに対する四...
  • 4 ファイルから読み取った改行文...
  • 5 C言語でヘッダファイルにグロー...
  • 7 C言語での引数の省略方法
  • 8 fgetsなどのときのstdinのバッ...
  • 9 char str[256]の256の意味は?
  • 10 配列を含む構造体の初期値について
  • 11 char*を初期化したいのですが
  • 12 fgetsで2行目から文字化け
  • 13 switch文のエラーについて
  • 14 戻り値で構造体を返すことは可...
  • 15 戻り値を返す関数の前に(void)...
  • 16 printfとputcharの違いは
  • 17 break文でループを一気に抜ける...
  • 18 C言語のポインタに直接アドレス...
  • 19 <math.h>があるのにsqrtが・...
  • 20 exeファイルの中身を見ることは...

デイリーランキング このカテゴリの人気デイリーQ&Aランキング

Jspやサーブレットでsystem.out..., [java]try 内の変数を外で!?, javaのdouble型の小数点以下の..., javaのエラーの意味が分かりま..., ネストされたオブジェクトを取..., 「続行するには何かキーを押し..., 会員情報登録プログラムの作成..., javaのコマンドライン引数を使..., eclipseでjava, コマンドプロンプトに不正な文..., javaでカレントディレクトリを..., java シンボルが見つかりません..., java教えてください。, javaでmidiを使って音を出したい, マンスリーランキング このカテゴリの人気マンスリーq&aランキング, ファイル名に変数を使う(java), 曜日の取得方法を教えて下さい!, エクセルマクロ文で、赤文字セ..., lc発振回路-ループ利得, javaのプログラムがどうしても....

質問して、直接聞いてみよう!

Q次郎

gooで dポイント がたまる!つかえる!

dアカウントでログイン

gooのご利用に応じてdポイントがたまる!つかえる!

ログインはdアカウントがおすすめです。 詳細はこちら

  • 新規登録する (無料)

教えて!gooの新規会員登録の方法が新しくなりました。

  • 1 gooID 新規登録
  • 3 ニックネーム 登録
  • 4 教えて!goo 会員登録 完了!

gooサービス全体で利用可能な「gooID」をご登録後、「電話番号」と「ニックネーム」の登録をすることで、教えて!gooの会員登録が完了となり、投稿ができるようになります!​

閉じる

gooIDにログイン済みです

教えて!goo 新規会員登録 (無料)

  • 1 gooID 新規登録 ログイン済
  • 2 電話番号 登録 次はここ!

閉じる

More than 5 years have passed since last update.

assignment to char from void makes integer from pointer without a cast wint conversion

【初心者向け】(値型の)値渡し、(値型の)参照渡し、参照型の値渡し、ポインタ型の値渡し(ポインタ渡し)

かなり初心者 advent calendar 1日目として.

開いていたから入れました。初心者向け1日目としては重くもしかしたら不適当な内容かもしれませんが是非知っておいてほしいことなので遅刻ながら初日を取らせてもらいました。 この記事は自分の失敗談を元にした 元記事 があります、本当は元記事に追記してこの記事にしようと思いましたがあまりにC++に寄りすぎていてかなり初心者にそぐわないものになると感じたので別記事にしました。 しかし内容はプログラマーなら誰しもが知っておいて欲しい内容だと思うのであえて投稿します。

C言語は誰しもが知っておくべき、C++はプロ用だは間違い

しばしそのようなことが言われている気がしますが実は逆だと思います。C++はC言語の拡張で高機能と言われますがC++出できることは頑張ればC言語でできます。しかしそれにはかなりの危険が伴います、その一例がポインタでC++は参照という高機能なものがあるので なるだけ ポインタというものは使わないようにと教えられます。 そしてしばしばC言語の入門サイトや入門書でポインタがあたかも参照であるかのような記述が見られます。しかし、ポインタと参照は全くの別物です、あえて誤解を恐れず言うならポインタはポイントしている(指している)値を参照できるものです。 では何故これが危険なのか見ていきましょう。

参照渡しはC言語が生まれる以前からあります。COBOLやFORTRANというものすごく古い言語にもあります。これらが生まれたのが1960年前後、C言語が生まれたのが1970年代と思うと参照渡しの歴史の深さが伺い知れます。 そしてきちんとした参照渡しは @shiracamus さんが提示してくれた 情報処理試験(徹底研究! 情報処理試験) に答えられます。 C言語のポインタは答えられますが非常に間違えやすいので注意が必要です。

さて早いですがラスボスの出現です。その前に確認です、ここまでの説明で変数がこのアドレスに格納されていてそれを指しているのがという低水準の話はしていません(よね)。 ポインタとはまさに高水準と低水準の橋渡しをするためのものです。 ここからはコードを出しましょう。

C言語(gcc、GNU Cコンパイラ)だと assignment makes integer from pointer without a cast [-Wint-conversion] という警告を出しつつもコンパイルが通りました警告の内容は int をポインタに置き換えてるけど大丈夫?です。 C++(g++、GNU C++コンパイラ)だと invalid conversion from ‘int’ to ‘int*’ [-fpermissive] というコンパイルエラーになります。意味は int をポインタになんかできるか馬鹿!です。 一見、C言語のほうが優しそうですがポインタまわりは罠だらけなのでC++のほうが親切です。 これの何が悪いのかというとポインタ型からint型への変換です。ポインタ型とint型はメモリサイズが同じなのでビット(2進数)まで見ると代入(assign)できますが符号とかいろいろ問題はあります。 なのでintからdoubleに置き換えましょう。

incompatible types when assigning to type ‘double’ from type ‘double *’ エラーです意味はdoubleはポインタ型 double* に変換できませんです。 ポインタ型の危険度を解説するためにだいぶ脇道にそれた感もありますが本論に戻りましょう。

ポインタ型の値渡しで参照渡しと同じことをしようとする(間違い編)

double* ptr_A=&A の部分はAのアドレスをアドレス演算子 & で取得しdouble型のポインタである ptr_A に代入しています。関数内に受け渡された時点で別のdouble型のポインタ C にアドレスの値がコピーされます。関数内部でCにDのアドレス値を代入していますが、関数内のCと関数の外にあるptr_Aは別の変数であるためptr_Aが影響を受けることはありません。 AとBは浮動点小数型なので小数値で表されます printf("A=%f B=%f \n", A, B) のfは浮動点小数(floating point number)を表しています。floatとdoubleは誰かが解説してくれると信じて先に進みます。 これを絵にすると次のようになります。

ポインタ型の値渡しで参照渡しと同じことをしようとする(正解編)

この間接演算子が定義されているのはC/C++以外で見たことがありません。(C++ではポインタは使うななので実質C言語のみです) 理由は明らかで参照は低水準を隠匿して高水準なものにしか触れられないようにしたのになぜ低水準から変数にアクセスする必要がるのか?です。 低水準にアクセスすることの対価が間違えれることです、私は間違えました、誰かがわざと間違えた構文を公開しないとも限りません。 なので間違いが起こせる構文よりは構文上で間違えられないようにするべきです。

C言語のみで語ることによる誤解

参照渡しの歴史はC言語より古いですがC系に参照が取り入れられたのはC++からで標準化が1998年のC++98で今よく使われているの多くの言語とほぼ同年代です。だからこれらの言語の基礎がC言語の文脈で語られるのはしかたがないことかもしれません。 しかし、C言語には参照渡しの概念がないので値型の値渡しを単に値渡し、ポインタ型の値渡しを単にポインタ渡しと言います。 さらに、C言語には参照型の概念もないのでポインタを参照と読ぶことが多々あります。それはC言語だけで語るなら正しいのかもしれませんが他言語を考慮に入れた時は必ずしも正しいとは言えません。 C言語では大きな構造体は値渡しでコピーを作るのはコストがかかるので構造体はポインタ(型の値)渡しが推奨されています。ポインタ(型の値)渡しではポインタ(int型と同サイズ)の コピー で済みます、ポインタ渡しでもコピーを発生させないわけではありません。 ここで一度C言語から離れて参照型の値渡しをみてみると値の実体自体をコピーしています、これはC言語を(のみ)知っている人にとっては高コストに感じられるのでポインタ(型の値)渡しを使っていると考えるのでしょう。ポインタを参照と呼ぶC言語の事情も相まってポインタ(型の値)渡しが参照(型の値)渡しとなり()を省略して参照渡しなったと考えられます。 更にポインタ型にはポイント先の値を直接代入できる * 間接演算子があるので参照渡しと 同じような ことができるのでよりポインタ(型の値)渡しが参照渡しと呼ばれることに拍車をかけているのではないかと訝っています。 他の言語のコンパイラやインタプリンタの内部実装を想像してみるとC言語のポインタを使っていてそれを参照型と読んでいるのかもしれませんがその場合もアドレスの値自身と * 演算子を隠匿して低水準にアクセスできないようにしてるのが普通だと思います。そもそもC言語で実装されている保証もありません。想像で語るなとはいいませんが想像で語ることには誤解を与える危険性もありますし自分も誤解をしている危険性もあります。

最後にC++での参照渡しを提示します。C++での参照型( T& )は値型に暗黙の型変換がされるので余計な演算子を書く必要がなくプログラマーが間違えにくくなっています。

初心者とC言語と低水準

私は初心者が低水準やC言語は必ずしも触る必要はないと考えています。 では何故こんな記事を書いたのかというと触れる必要はなくとも知っておいてほしいからです。 C言語やC++は高級言語ながら低水準にアクセスできる便利な言語です。今回て提示したコードは注意書きがない限り gcc/g++ -Wall -Wextra ファイル名 で警告なしのコンパイルが可能です、gccはC言語コンパイラ、g++はC++コンパイラです。Linux/Unixならたいてい入っていますし。 Windowsでも入れるのはそう難しくないはずです 。 今回は説明で端折った部分もあるのでもっと詳しく知りたい場合は元記事を見てもらうか自分で試してみてください。 そしてC/C++はこうした泥臭いことが得意なのでカッコいいGUIアプリや3Dゲーム、Webアプリを作りたいとなると不向きな言語です。

Register as a new user and use Qiita more conveniently

  • You get articles that match your needs
  • You can efficiently read back useful information
  • You can use dark theme

IMAGES

  1. c

    assignment to char from void makes integer from pointer without a cast wint conversion

  2. assignment makes integer from pointer without a cast

    assignment to char from void makes integer from pointer without a cast wint conversion

  3. Array : Assignment makes integer from pointer without a cast [-Wint

    assignment to char from void makes integer from pointer without a cast wint conversion

  4. C : warning: assignment makes pointer from integer without a cast [enabled by default]

    assignment to char from void makes integer from pointer without a cast wint conversion

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

    assignment to char from void makes integer from pointer without a cast wint conversion

  6. Assignment makes pointer from integer without a cast ошибка

    assignment to char from void makes integer from pointer without a cast wint conversion

VIDEO

  1. Faceless Void makes fun of Weaver #dota2 #shorts #facelessvoid

  2. #012

  3. Why Tipping Watson's Void Was a HUGE Mistake for SATANIC

  4. Maw of the Void speedrun 3:07

  5. Программирование на Си. #6. Первая программа

  6. SQL Server CAST Function Explained with Example

COMMENTS

  1. Assignment makes pointer from integer without cast

    However, this returns a char. So your assignment. cString1 = strToLower(cString1); has different types on each side of the assignment operator .. you're actually assigning a 'char' (sort of integer) to an array, which resolves to a simple pointer. Due to C++'s implicit conversion rules this works, but the result is rubbish and further access to ...

  2. c error: assignment makes integer from pointer without a cast [-Werror

    Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.

  3. c

    1. Earlier, I asked a similar question, but I've since changed my code. Now the compiler gives me a different warning. This is an example of what my code looks like now: void *a = NULL; void *b = //something; a = *(int *)((char *)b + 4); When I try to compile, I get "warning: assignment makes pointer from integer without a cast."

  4. Assignment makes integer from pointer without a cast in c

    Case 1: Assignment of a pointer to an integer variable. n1 = 2; ptr = &n1; n2 = ptr; /* Failure in this line */. In this simple code we have three variables, an integer pointer "ptr", and two ...

  5. [SOLVED] C

    Re: C - assigment makes integer from pointer without a cast warning. path [1] is the second element of a char array, so it's a char. home is a char *, i.e. a pointer to a char. You get the warning because you try to assign a char* to a char. Note also the potential to overflow your buffer if the content of the argv [1] argument is very long.

  6. How to fix "warning: assignment to 'int (*)(int, int, int, void

    How to fix "warning: assignment to 'int (*)(int, int, int, void *)' from 'int' makes pointer from integer without a cast [-Wint-conversion]" I am currently reading a book on Ethical Hacking and I have hit a road block with this warning. The book uses C for this example in Sys call hooking, and I seem to keep getting this warning message ...

  7. assignment to 'char *' from 'int' makes pointer from integer without a

    p = itoa (num); You probably want: p = itoa (*num); Since otherwise you're sending a pointer to an int, and not the int. But I don't think itoa () even works that way. You'd also have to send in the string buffer. You should go check out a reference. 1. I'm new to C language: include "main.h" int func_d (va_list li) { char *p; int *num; int ...

  8. assignment makes integer from pointer without a cast

    text should be declared as: char *text = NULL; You need a pointer to char. This pointer can be set to point to any literal string (as you do in your case statements). char text; // this is just a single character (letter), not a string. 2. Objective_Ad_4587 • 3 yr. ago. i got it thank you very much.

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

    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: A character string literal has static storage duration and type "array of char", and is initialized with the given characters. A wide string literal has static storage duration and type "array of wchar_t", and is initialized with the wide characters corresponding to ...

  10. 问题:warning: assignment makes integer from pointer without a cast

    C语言在编译过程中有时候会报警告:. warning: assignment makes integer from pointer without a cast [enabled by default] 这个警告其实不会导致系统运行出错,警告的意思是赋值类型和变量类型不一致导致的。. 在这个问题中一般出现的地方如下:. tempStruct *temp = tempStructGet ...

  11. Incompatible types in assignment of 'uint8_t {aka unsigned char}' to

    atoi is for converting a textual representation of a number ("123" for example) into an actual integer.. The c_str() function gives you a pointer to the internal buffer of the String (assuming you actually have a String) which is no different to a uint8_t[] or uint8_t * (other than the signedness).. Without knowing exactly what the destination function for this buffer requires it's very hard ...

  12. C语言assignment makes pointer from integer without a cast

    C语言assignment makes pointer from integer without a cast. 这个警告的意思是将一个int整数值直接赋值给了一个指针变量。. ( 重点是类型不一致 ). 消除警告的方法就是明确类型转换是否是正确的,如果确实要把整数变量赋予指针变量,那么请使用强制类型转换。. 否则 ...

  13. C error

    n.c: In function 'main': n.c:14:18: warning: assignment makes integer from pointer without a cast [enabled by default] firstInitial = "J"; ^. n.c:15:19: warning: assignment makes integer from pointer without a cast [enabled by default] secondInitial = "R"; ^. n.c:29:18: warning: assignment makes integer from pointer without a cast [enabled by ...

  14. assignment makes integer from pointer without a cast

    assignment makes integer from pointer without a castc/c++ warning explained#syntax #c/c++ #compiler #error #warning

  15. compiler warning: pointer from integer without a cast

    and the following warnings: passing argument 2 of 'sd_ppi_channel_assign' makes pointer from integer without a cast [-Wint-conversion] passing argument 3 of 'sd_ppi_channel_assign' makes pointer from integer without a cast [-Wint-conversion] All seems to work, but would appreciate understanding the warnings. Many thanks,

  16. Makes Pointer From Integer Without a Cast: Fix It Now!

    How To Stop a Pointer Creation From an Integer Without a Cast. - Use Equal Data Types During Assignment. - Ensure the Pointer and Integer Have the Same Sizes. - Pass a "Format String" to the "Printf ()" Function. - Use a Function That Returns a Pointer to "Struct _IO_file". - Copy the String to Character Array or Character ...

  17. "assignment makes integer from pointer without a cast -wint-conversion

    Strings literals are pointers in C. When you a sentence like "Hello world!", that variable is of the type const char*, meaning it is a pointer to a bunch of chars that cannot be modified. When you use double-quotes, you are making a const char*, even if there is only one character. You should use single-quotes if you want to have a char.

  18. C言語のポインターに関する警告

    warning: assignment makes integer from pointer without a cast という警告がでます。 ポインターは使っていないのですが、ポインターに関する警告が出ているようで困っています。 どこが悪いのかまったくわからなくて作業が完全に止まってしまいました。

  19. assignment to 'char' from 'char *' makes integer from pointer without a

    Second, strcat returns an pointer to a char (array), i.e.: char *. So, you are trying to assign a pointer in a place that expects a value. So, you are trying to assign a pointer in a place that expects a value.

  20. 【初心者向け】(値型の)値渡し、(値型の)参照渡し、参照型の値渡し、ポインタ型の値渡し(ポインタ渡し)

    C言語(gcc、GNU Cコンパイラ)だとassignment makes integer from pointer without a cast [-Wint-conversion]という警告を出しつつもコンパイルが通りました警告の内容はintをポインタに置き換えてるけど大丈夫?です。 C++(g++、GNU C++コンパイラ)だとinvalid conversion from 'int' to 'int*' [-fpermissive]というコンパイル ...