logo
down
shadow

Why pointers can't be used to index arrays?


Why pointers can't be used to index arrays?

By : Tae Chon
Date : October 21 2020, 06:10 PM
I hope this helps you . The difference is that char A[] defines an array and char * does not.
The most important thing to remember is that arrays are not pointers.
code :
char *A = "ab";
const char *A = "ab";
char A[] = "ab";
#include <stdio.h>
int main(void) {
    char array_object[] = "ab"; /* array_object is writable */
    char *ptr = array_object;   /* or &array_object[0] */
    printf("array_object[0] = '%c'\n", array_object[0]);
    printf("ptr[0] = '%c'\n", ptr[0]);
}
array_object[0] = 'a'
ptr[0] = 'a'


Share : facebook icon twitter icon
finding an index to pointers to chars arrays

finding an index to pointers to chars arrays


By : beingzen
Date : March 29 2020, 07:55 AM
With these it helps sizeof is known as a compile-time operator. It can count sizes only of objects whose size can be determined before-hand. So when you pass it a pointer (arrays degenerate to pointers when passed as function arguments), you just get the size of the pointer.
A typical arrangement is to end the list with a NULL pointer. With such a list, your function could be written this way:
code :
int indexof(char *aword, char *arrayofwords[]){
  int i;
  for (i=0 ; arrayofwords[i]!=NULL ;i++){
    if (strcmp(aword,arrayofwords[i])==0){return i;}}
  return -1;//not found
}
#include <stdlib.h>

#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))

int main() {
char *ta[]={"asdf","qwer","zxcv"};
char *aword="qwer";
  int i; unsigned int ct=COUNT_OF(ta);
  for (i=0 ; i<ct ;i++){
    if (strcmp(aword,ta[i])==0){return i;}}
  return -1;//not found
}
#include <stdlib.h>

#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))

int main() {
char *ta[]={"asdf","qwer","zxcv"};
char *word="qwer";
  return indexof(word, ta, COUNT_OF(ta));
}

int indexof(char *aword, char *arrayofwords[], int length){

  int i; unsigned int ct=length;
  for (i=0 ; i<ct ;i++){
    if (strcmp(aword,arrayofwords[i])==0){return i;}}
  return -1;//not found
}
How to index arrays using pointers safely

How to index arrays using pointers safely


By : Keighan robichaud
Date : March 29 2020, 07:55 AM
Does that help Your understanding of what the text recommends is correct, as is your implementation. But regarding the basis of the recommendation, I think you are confusing safe with correct.
It's not that using a pointer is safer than using an index. The argument is that, in reasoning about the code, it is easier to decide that the logic is correct when using pointers. Safety is about failure modes: what happens if the code is incorrect (references a location outside the array). Correctness is more fundamental: that the algorithm provably does what it sets out to do. We might say that correct code doesn't need safety.
Index parameter type for operator[] on pointers and arrays

Index parameter type for operator[] on pointers and arrays


By : Abdul
Date : March 29 2020, 07:55 AM
To fix this issue operator [] on pointers can be any integral expression
To be general, ptrdiff_t is technically what you want, if the intention is for data and data + i to be any two arbitrary pointers in either other and to arbitrary blocks of memory, since ptrdiff_t is defined as a signed type for holding the difference between two pointers (however, it's not guaranteed to not overflow: ptrdiff_t on machines with 32-bit pointers is typically 32-bit, not the next signed integral size higher)
Pointers and arrays without using index

Pointers and arrays without using index


By : Asampaiz
Date : March 29 2020, 07:55 AM
it fixes the issue "which works perfectly" : this is impossible
(*nNumArray <= &nNumArray[nSize-1]) : you compare an int into the vector (and later outside it) and an address of an int into the vector ( and in fact after its end). A priori your compiler signal the error
code :
#include <stdio.h>

int sumArray(int nNumArray[], int nSize)
{
    int nSum = 0;
    int * ptr = nNumArray;

    while(ptr <= &nNumArray[nSize-1])
    {
        nSum += *ptr++;
    }
    return nSum;
}

int main()
{
  int a[3] = {1,2,3};

  printf("%d\n", sumArray(a, 3));
  return 0;
}
How to index array of pointers to arrays [queue]?

How to index array of pointers to arrays [queue]?


By : Mahmud Parvege uzzal
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Well, it's still arr[3][2].
Although arrays are not pointers, the way we use them is effectively using a pointer because of the way they work and the way their name decays.
Related Posts Related Posts :
  • Random function stuck on one number
  • printf following zeros to a certain degree
  • Measuring CPU clocks consumed by a process
  • How is a CRC32 checksum calculated?
  • Tips on redefining a register bitfield in C
  • Converting a program from c to bash script
  • Static library not included in resulting LLVM executable
  • What is the meaning of "wild pointer" in C?
  • Reading DWORD from binary file
  • Efficient way to find divisibility
  • Whats the fastest, portable way to hash pointers we know are pointer aligned to a fixed size int?
  • Why does my summation program behaves weird?
  • How to determine values saved on the stack?
  • Thread scheduling C
  • C - converting to 2s complement
  • wireless networks c program
  • From where does the uboot get the information about flash, RTC, RAM in uboot/kernel development
  • printf behaviour in C
  • Measure size and way-order of L1 and L2 caches
  • Efficient copy of entire directory
  • Efficient way to create/unpack large bitfields in C?
  • getnameinfo prototype asks for sockaddr not sockaddr_in?
  • Linked List Sorting with Strings In C
  • Typecasting a floating value or using the math.h floor* functions?
  • Problem implementing sorting algorithm in C with an array of structs
  • How to copy a ram_base file to disk efficiently
  • How to fix casting a pointer to an integer?
  • How to round floating point numbers to the nearest integer in C?
  • PIC C - Sending 200 values over USB, but it only sends 25 or so of them
  • How do you return a string from a function correctly in Dynamic C?
  • Monotonic clock on IRIX
  • setsockopt (sys/socket.h)
  • How to make C program wait (on Linux)?
  • i find a problem using atoi() method in ansi c?
  • Why is this array being initialized in an odd way?
  • How to execute commands in chroot?
  • Microsecond (or one ms) time resolution on an embedded device (Linux Kernel)
  • Question about variable definitions in functions
  • SMTP on C: STARTTLS via OpenSSL
  • fetch value of ipaddress
  • Binary expressions in C
  • Why do char * and char ** have the same value?
  • problem in allocating kernel memory by malloc() from user space?
  • dangling pointer, reason for value change after free()?
  • How to compile C program with multiple files in Xcode
  • Can you make an incrementing compiler constant?
  • Reading data from text file in C
  • SQLite's test code to production code ratio
  • Displaying an inverted pyramid of asterisks
  • sd spi mode in C language
  • printf("%2d") in C programming
  • Add a certain number of bytes to a void pointer in C
  • How should I protect against hard link attacks?
  • What's the difference between unsigned long/long/int in c/c++?
  • Get an input from keyboard without 'return' in C
  • How to convert if to switch-case in c
  • C: swapping two pointer value
  • What does \x mean in c/c++?
  • run a command line argument prog
  • Pass a dynamic structure by reference? [C]
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk