簡體   English   中英

ISO C++ 禁止指針和整數之間的比較 [-fpermissive]| [C]

[英]ISO C++ forbids comparison between pointer and integer [-fpermissive]| [C]

我正在嘗試使用 Dev-C++ 5.9.2 在 Ubuntu(64 位)上編譯以下代碼

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// Afficher les nombres parfaits inférieurs ou égaux à un entier donné
void parfaitInf(int n)
{
    int i, j, somme;
    for(i=2;i<=n;i++)
    {
        somme=1;
        for(j=2;j<=i/2;j++)
        {
            if(i%j==0) somme+=j;
        }
        if(somme==i) printf("%d\n",i);
    }
}

// Tester si un entier est un nombre d'Armstong
void armstrong(int n)
{
    int somme=0,m=n;
    do
    {
        somme+=pow(m%10,3);
        m/=10;
    }
    while(m!=0);
    if(somme==n) printf("%d est Armstrong.\n",n);
    else printf("%d n\'est pas Armstrong !\n",n);
}

// Calculer la racine carrée d'un nombre entier
void racineCarree(int n)
{
    printf("La racine carr%ce de %d est %f",130,n,sqrt(n));
}

void menuPrincipale(int *choix)
{
    do
    {
    printf("\t\tMenu\n");
    printf("[1] Afficher les nombres parfaits inf%rieurs ou %cgaux %c un entier donn%ce\n",130,130,133,130);
    printf("[2] Tester si un entier est un nombre d\'Armstrong\n");
    printf("[3] Calculer la racine carr%ce d\'un nombre entier\n\n",130);
    printf("Votre choix ? ");
    scanf("%d",&choix);
    }
    while(&choix<1 || &choix>3);
}

int main()
{
    int n,choix;
    menuPrincipale(choix); //compilation error here
    printf("%d",&choix);
    // Not Continued
    return 0;
}

在第 51 行,我的編譯器給了我錯誤“ISO C++ 禁止比較指針和整數 [-fpermissive]”。

在第 57 行,我的編譯器給了我錯誤“[Error] invalid conversion from 'int' to 'int*' [-fpermissive]”

為什么這不起作用? 我想了解當前的問題。

指針基礎

int i = 1;
int *p = &i; // p is a memory address of where i is stored.  & is address of variable.

int j = *p; // j is the value pointed to by p.  * resolves the reference and returns the value to which the pointer addresses.

int **dp = &p;  // dp is the address of the pointer that itself is an address to a value.  dp->p->i

cout << i; // 1
cout << p; // 0x.... some random memory address
cout << j; // 1
cout << dp; // 0x... some random memory address that is where the p variable is stored

所以作為你的問題,正如其他人在 menuPrincipale() 中所說的那樣,你錯誤地使用了指針。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM