指针不希望在C中通过引用传递

时间:2013-10-14 10:56:41

标签: reference

我有一点问题。我想采用函数中使用的变量“posLigne”。这是

     #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
     #define TAILLE_VIDE 30
     #define TAILLE_MAX 1000
#include <ctype.h>
int ar(char mot);
static int compare (void const *a, void const *b);
void indexation(FILE *f,char tChar[][100],char filename[],int *posLigne);
int motVide(char *mot);

    int main()
    {
    FILE *f=NULL;
    int posLigne=1;
    f=fopen("test.txt","r+");
int i;
char tChar[100][100];
char X[100][100];
int maju[100];
indexation(f,tChar,"test.txt",&posLigne);
charaff(tChar,7);
printf("%d",pos(tChar[1]));



    return 0;
}
void indexation(FILE *f,char tChar[][100],char filename[],int *posLigne)
{
    char nbr[100]="";
    int i=0,j=0,l=1,k=0;
    char ligne[TAILLE_MAX]="";
    char mot[100]="";
    while (fgets(ligne,TAILLE_MAX,f))
    {
            i=0;
            while (ligne[i]!='\0')
            {




memset (mot, 0, sizeof (mot));

    while (!(ar(ligne[i])))
    {

        mot[k]=ligne[i];
        k++;i++;
    }
        k=0;

//    if (!(motVide((mot))))
if(!motVide(mot))
        {
            strcat(tChar[j],mot);
            strcat(tChar[j]," ");
            strcat(tChar[j],filename);
            strcat(tChar[j]," ");
            sprintf(nbr,"%d",l);
            strcat(tChar[j],nbr);
            strcat(tChar[j]," ");
            sprintf(nbr,"%d",*posLigne);
            strcat(tChar[j],nbr);
            *posLigne++;
            j++;



        }
        i++;
    }
    l++;
    }
}

只关注变量“posLigne”,当我想将它用作局部变量时,它完美无缺。但是当我想通过使用指针传递引用时,它会显示大数字。

谢谢你,祝你有个美好的一天

2 个答案:

答案 0 :(得分:1)

这是因为*posLigne++;中的运算符的优先级。

尝试(*posLigne)++;

答案 1 :(得分:0)

你的路线;

*posLigne++;

...可能不会做你认为它做的事情。由于operator precedence,它将等同于;

*(posLigne++);

...这将改变指针,而不是指向的值。要实际增加该值,您需要使用parenteses;

(*posLigne)++;
相关问题