incompatible pointer assignment ?

  • Thread starter zmbdcqnrdfetnws
  • Start date
Z

zmbdcqnrdfetnws

Hello,

I have a question; Can someone explain this beginning
programmer why the following pointer `ptr' assigment
to the array of pointers to strings `*palpha[]' won't work ?

#include <stdio.h>
#include <ctype.h>

int main(void) {
const char *palpha[]= { "alpha", "bravo", "charlie", "delta" };
char *ptr = NULL;
int i = 0;

for(i = 0; i < 4; i++) {
/* this doesn't seem to work ? */
ptr = palpha;

while(*ptr++)
printf("%c", toupper(*ptr));

printf("\n");
}

return 0;
}

If anyone could explain the reasoning behind my faillure
I would be greatfull....

Thnkx..

J.
 
E

Ed Morton

zmbdcqnrdfetnws wrote:
int main(void) {
const char *palpha[]= { "alpha", "bravo", "charlie", "delta" };
char *ptr = NULL;

const char *ptr;
while(*ptr++)
printf("%c", toupper(*ptr));

while (*ptr)
printf("%c", toupper(*ptr++));

<snip>

Your palpha is an arry of const strings, so ptr should point to a const
string, then in your loop, you incremented ptr before the first call to
toupper.

Regards,

Ed.
 
T

Thomas Stegen

zmbdcqnrdfetnws said:
Hello,

I have a question; Can someone explain this beginning
programmer why the following pointer `ptr' assigment
to the array of pointers to strings `*palpha[]' won't work ?

#include <stdio.h>
#include <ctype.h>

int main(void) {
const char *palpha[]= { "alpha", "bravo", "charlie", "delta" };

palpha is an array of pointers to const char.
char *ptr = NULL;

ptr is a pointer to char (no const). So that is your problem
down...
for(i = 0; i < 4; i++) {
/* this doesn't seem to work ? */
ptr = palpha;


....here.

Change the type of the pointer to pointer to const char and you should
be set.
 
D

Dan Pop

In said:
I have a question; Can someone explain this beginning
programmer why the following pointer `ptr' assigment
to the array of pointers to strings `*palpha[]' won't work ?

#include <stdio.h>
#include <ctype.h>

int main(void) {
const char *palpha[]= { "alpha", "bravo", "charlie", "delta" };
char *ptr = NULL;
int i = 0;

for(i = 0; i < 4; i++) {
/* this doesn't seem to work ? */
ptr = palpha;


You may want to use a compiler with better diagnostics:

fangorn:~/tmp 125> gcc test.c
test.c: In function `main':
test.c:11: warning: assignment discards qualifiers from pointer target type

This make it quite clear, doesn't it?

Either drop the const from the declaration of palpha or add a const in the
declaration of ptr. You can also use a cast to make the compiler happy,
but that's usually not a good idea.

Dan
 
Z

zbqdxdm

Hello,
I have a question; Can someone explain this beginning
programmer why the following pointer `ptr' assigment
to the array of pointers to strings `*palpha[]' won't work ?

<CUT>... </CUT>

Thnkx for the quick and clear reply you guys...,
I know now what I did wrong and I have solved the
problem.

Thanks again .. !

J.
 
A

A. Sinan Unur

Hello,

I have a question; Can someone explain this beginning
programmer why the following pointer `ptr' assigment
to the array of pointers to strings `*palpha[]' won't work ?

That question was answered by others.
while(*ptr++)
printf("%c", toupper(*ptr));

Do you skip the first character of each word intentionally?

Sinan.
 
C

CBFalconer

zmbdcqnrdfetnws said:
I have a question; Can someone explain this beginning
programmer why the following pointer `ptr' assigment
to the array of pointers to strings `*palpha[]' won't work ?

#include <stdio.h>
#include <ctype.h>

int main(void) {
const char *palpha[]= { "alpha", "bravo", "charlie", "delta" };
char *ptr = NULL;
const char *ptr = NULL;
^^^^^
int i = 0;

for(i = 0; i < 4; i++) {
/* this doesn't seem to work ? */
ptr = palpha;

while(*ptr++)

while (*ptr)
printf("%c", toupper(*ptr));
printf("%c", toupper((unsigned char)*ptr++));
^^^^^^^^^^^^^^^
printf("\n");
}

return 0;
}

Make the indicated changes. The underlined portions are probably
not absolutely necessary. Then think about the difference.
 
B

Barry Schwarz

zmbdcqnrdfetnws wrote:
int main(void) {
const char *palpha[]= { "alpha", "bravo", "charlie", "delta" };
char *ptr = NULL;

const char *ptr;
while(*ptr++)
printf("%c", toupper(*ptr));

while (*ptr)
printf("%c", toupper(*ptr++));

<snip>

Your palpha is an arry of const strings, so ptr should point to a const

Almost. palpha is an array of pointers to const strings.
string, then in your loop, you incremented ptr before the first call to
toupper.

Regards,

Ed.



<<Remove the del for email>>
 
M

Martin Ambuhl

zmbdcqnrdfetnws said:
Hello,

I have a question; Can someone explain this beginning
programmer why the following pointer `ptr' assigment
to the array of pointers to strings `*palpha[]' won't work ?


#include <stdio.h>
#include <ctype.h>

int main(void)
{
const char *palpha[] = { "alpha", "bravo", "charlie", "delta" };
char *ptr = NULL;
int i = 0;

for (i = 0; i < 4; i++) {
/* this doesn't seem to work ? */
/* mha: the palpha point to const char, while ptr points to
(non-const) char. You are discarding the constness of the
target. That said, it is unclear what you mean by "doesn't seem
to work."

If the loop following, which skips over the first character, is
what "doesn't seem to work," that may be because you are
incrementing ptr before use. A replacement follows. */
#if 0
/* OP's code */
ptr = palpha;
while (*ptr++)
printf("%c", toupper(*ptr));
#endif
/* mha: one of many possible replacements, still discarding
constness. Fixing that is left as an exercise. */
for (ptr = palpha; *ptr; ptr++)
putchar(toupper(*ptr));

printf("\n");
}

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top