• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

compile error, pointer declaration

I am brand new to pointers and am pretty new to coding in general.  I am going through a pointers tutorial and a suggested exercise is to write a program that copies a specified number of integers from one array to another array. I have many errors when I compile but I will start w/1st and separate them as I have been recommended to do.
#include <stdlib.h>
#include <stdio.h>
int intsA[5] = {9,9,9,9,9};
int intsB[5] = {0,0,0,0,0};
int *pointerA, *pointerB;
pointerA = intsA;

Open in new window


I get the following errors on line 6:

tut_intcopy.c:6: warning: data definition has no type or storage class
tut_intcopy.c:6: error: conflicting types for ‘pointerA’

I cannot figure out why this is happening.  I thought I declared the pointer type with the "int" syntax.  
0
dazedandconfused69
Asked:
dazedandconfused69
  • 3
1 Solution
 
evilrixSenior Software Engineer (Avast)Commented:
That code  builds just fine for me... the error must be related to something else.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Can you post the actual code please?
0
 
dazedandconfused69Author Commented:
Sorry - here is the entire code (yes, I know I have other errors but splitting up the questions) and the 4 errors associated with the section before main and another function I am including:

errors:


tut_intcopy.c:6: warning: data definition has no type or storage class
tut_intcopy.c:6: error: conflicting types for ‘pointerA’
tut_intcopy.c:5: error: previous declaration of ‘pointerA’ was here
tut_intcopy.c:6: warning: initialization makes integer from pointer without a cast

full code:
#include <stdlib.h>
#include <stdio.h>
int intsA[5] = {9,9,9,9,9};
int intsB[5] = {0,0,0,0,0};
int *pointerA, *pointerB;
pointerA = intsA;

int nbr;

void int_copy(int *ptriA, int *ptriB, int nbr)
{
   while (nbr != 0)
   {
      *pointerB++ = *pointerA++;
      nbr--;
   }
   *pointerB = '\0';
   return (0);
}   

int main()
{
    nbr = 3;	
    int_copy(pointerB, pointerA, nbr);
    for(int i=0;i<(sizeof(intsB)); i++);
       printf("%d ",intsB[i]));
}    

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
Move the initialisation of pointerA inside main and the problem should go away.
#include <stdlib.h>
#include <stdio.h>

int intsA[5] = {9,9,9,9,9};
int intsB[5] = {0,0,0,0,0};
int *pointerA, *pointerB;

int main()
{
   pointerA = intsA;
}

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now